HUD 1874 畅通工程续(Dijkstra/spfa/floyd)

来源:互联网 发布:淘宝第三方鉴定机构 编辑:程序博客网 时间:2024/06/10 06:52
Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
 

Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
 

Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
 

Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
 

Sample Output
2-1
 
版本一:Floyd(时间复杂度O(n^3))
<span style="font-size:10px;">//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<25#define NN 1000006using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************练手很好,可以尝试 Dijkstra ,spfa,Floyd尝试1:Floyd**********************************************************************/int s,t;//起点和终点int n,m;//n个点,m条边int d[202][202];void init(){    for (int i = 0; i < n; i++)    {        for (int j = 0; j < n; j++)        {            d[i][j] = inf;        }        d[i][i] = 0;    }}void floyd(){    for (int k = 0; k < n; k++)    {        for (int i = 0; i < n; i++)        {            for (int j = 0; j < n; j++)            {                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);            }        }    }}int main(){    while (cin>>n>>m)    {        init();        for (int i = 0,u,v,w; i < m; i++)        {            scanf("%d%d%d",&u,&v,&w);            if (w < d[u][v])            {                d[u][v] = w;                d[v][u] = w;//双向            }        }        cin>>s>>t;        floyd();        if (d[s][t] == inf)        {            puts("-1");        }        else        {            printf("%d\n",d[s][t]);        }    }    return 0;}</span><span style="font-size:18px;"></span>
版本二:SPFA(不定长数组的邻接矩阵实现)
<span style="font-size:10px;">//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<25#define NN 1000006using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************练手很好,可以尝试 Dijkstra ,spfa,Floyd尝试2:SPFA(不定长数组实现邻接矩阵)**********************************************************************/int s,t;//起点和终点int n,m;//n个点,m条边struct Node{    int v,w;}a;bool vis[202];vector<Node>u[202];queue<int>q;int d[202];void spfa(){    for (int i = 0;i < n;i++)        d[i] = inf;    mem(vis,0);    q.push(s);    vis[s] = 1;    d[s] = 0;//源点最先入队并标记    while (!q.empty())    {        int head = q.front();//取队首元素        q.pop();        vis[head] = 0;        for (int i = 0;i < u[head].size();i++)        {            if (d[ u[head][i].v ] > d[head] + u[head][i].w)//松弛成功            {                d[ u[head][i].v ] = d[head] + u[head][i].w;//更新值                if (vis[u[head][i].v] == 0)                {                    q.push(u[head][i].v);//入队                    vis[u[head][i].v] = 1;                }            }        }    }}int main(){    while (cin>>n>>m)    {        for (int i = 0;i < n;i++)//不定长数组记得清空            u[i].clear();        for (int i = 0,uu; i < m; i++)        {            scanf("%d%d%d",&uu,&a.v,&a.w);            u[uu].push_back(a);            Node b;            b.v = uu,b.w = a.w;            u[a.v].push_back(b);        }        cin>>s>>t;        spfa();        if (d[t] == inf) puts("-1");        else printf("%d\n",d[t]);    }    return 0;}</span>


版本三:SPFA(二维数组实现邻接矩阵,这题点少,用二维数组时间反而比不定长数组少)
<span style="font-size:10px;">//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<25#define NN 1000006using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************练手很好,可以尝试 Dijkstra ,spfa,Floyd尝试2:SPFA(二维数组实现邻接矩阵)**********************************************************************/int s,t;//起点和终点int n,m;//n个点,m条边bool vis[202];int mp[202][202];queue<int>q;int d[202];void spfa(){    for (int i = 0; i < n; i++)        d[i] = inf;    mem(vis,0);    q.push(s);    vis[s] = 1;    d[s] = 0;//源点最先入队并标记    while (!q.empty())    {        int head = q.front();//取队首元素        q.pop();        vis[head] = 0;        for (int i = 0; i < n; i++)        {            if (d[i] > d[head] + mp[head][i])//松弛成功            {                d[i] = d[head] + mp[head][i];//更新值                if (vis[i] == 0)                {                    q.push(i);//入队                    vis[i] = 1;                }            }        }    }}int main(){    while (cin>>n>>m)    {        for (int i = 0; i < n; i++)        {            for (int j = 0; j < n; j++)            {                mp[i][j] = inf;            }            mp[i][i] = 0;        }        for (int i = 0,uu,vv,ww; i < m; i++)        {            scanf("%d%d%d",&uu,&vv,&ww);            if (ww < mp[uu][vv])            {                mp[uu][vv] = ww,mp[vv][uu] = ww;            }        }        cin>>s>>t;        spfa();        if (d[t] == inf) puts("-1");        else printf("%d\n",d[t]);    }    return 0;}</span>

版本四:SPFA(邻接表实现)

//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<25#define NN 1000006using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************练手很好,可以尝试 Dijkstra ,spfa,Floyd尝试2:SPFA(邻接表实现)**********************************************************************/int s,t;//起点和终点int n,m;//n个点,m条边struct Node{    int v,w,next;}e[2002];//因为双向的原因边最多有2000条int h[102];bool vis[202];queue<int>q;int d[202];void spfa(){    for (int i = 0; i < n; i++)        d[i] = inf;    mem(vis,0);    q.push(s);    vis[s] = 1;    d[s] = 0;//源点最先入队并标记    while (!q.empty())    {        int head = q.front();//取队首元素        q.pop();        vis[head] = 0;        for (int i = h[head]; i != -1; i = e[i].next)        {            if (d[e[i].v ] > d[head] +e[i].w)//松弛成功            {                d[e[i].v ] = d[head] + e[i].w;//更新值                if (vis[e[i].v] == 0)                {                    q.push(e[i].v);//入队                    vis[e[i].v] = 1;                }            }        }    }}int main(){    while (cin>>n>>m)    {        mem(h,-1);        int o = 0;        for (int i = 0,uu,vv,ww; i < m; i++)        {            scanf("%d%d%d",&uu,&vv,&ww);            e[o].v = vv;            e[o].w = ww;            e[o].next = h[uu];            h[uu] = o++;            e[o].v = uu;            e[o].w = ww;            e[o].next = h[vv];            h[vv] = o++;        }        cin>>s>>t;        spfa();        if (d[t] == inf) puts("-1");        else printf("%d\n",d[t]);    }    return 0;}
版本五:Dijkstra
//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<25#define NN 1000006using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************练手很好,可以尝试 Dijkstra ,spfa,Floyd尝试3:Dijkstra**********************************************************************/int s,t;//起点和终点int n,m;//n个点,m条边bool vis[202];int d[202];int mp[202][202];void Dijk(){    mem(vis,0);    for (int i = 0; i < n; i++)    {        int mn = inf,pos;        for (int j = 0; j < n; j++)        {            if (d[j] < mn&&vis[j] == 0)            {                mn = d[j];                pos = j;            }        }        vis[pos] = 1;        for (int j = 0; j < n; j++)        {            d[j] = min(d[j],d[pos]+mp[pos][j]);        }    }}int main(){    while (cin>>n>>m)    {        for (int i = 0;i < n;i++)        {            for (int j = 0;j < n;j++)            {                mp[i][j] = inf;            }            mp[i][i] = 0;            d[i] = inf;        }        for (int i = 0,uu,vv,ww; i < m; i++)        {            scanf("%d%d%d",&uu,&vv,&ww);            if (ww < mp[uu][vv])            {                mp[uu][vv] = ww,mp[vv][uu] = ww;            }        }        cin>>s>>t;        d[s] = 0;        Dijk();        if (d[t] == inf) puts("-1");        else printf("%d\n",d[t]);    }    return 0;}



1 0
原创粉丝点击