# HDU 1874 畅通工程续 （Floyd, Dijkstra，Bellman-Ford，SPFA）

Time Limit:
1000MS
Memory Limit:
32768KB
64bit IO Format:
%I64d & %I64u
Submit
Status

Description

Input

Output

Sample Input

Sample Output

Hint

Description

Input

Output

Sample Input

```3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2```

Sample Output

```2
-1```
`解法一：Dijkstra算法`
``````<pre class="html" name="code">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf 1<<29
#define maxn 210
int maps[maxn][maxn],dis[maxn],vis[maxn];
int n,m;
using namespace std;

int dijkstra(int s,int t)
{
for(int i = 0;i < n;i++)
{
dis[i] = maps[s][i];
vis[i] = 0;
}
dis[s] = 0;
vis[s] = 1;

int temp,k;
for(int i = 0;i < n;i++)
{
temp = inf;
for(int j = 0;j < n;j++)
{
if(vis[j] == 0 && temp > dis[j])
{
k = j;
temp = dis[j];
}
}
if(temp == inf)
break;
vis[k] = 1;
for(int j = 0;j < n;j++)
{
if(dis[j] > dis[k] + maps[k][j])
dis[j] = dis[k] + maps[k][j];
}
}
if(dis[t] == inf)
return -1;
else
return dis[t];

}

int main()
{
int a,b,x,s,t,ans;
while(scanf("%d %d",&n,&m) != EOF)
{
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
maps[i][j] = (i == j ? 0 : inf);
while(m--)
{
scanf("%d %d %d",&a,&b,&x);
if(x < maps[a][b])
maps[a][b] = maps[b][a] = x;
}
scanf("%d %d",&s,&t);
ans = dijkstra(s,t);
printf("%d\n",ans);
}
return 0;
}
``````
``</pre></div><div class="textBG"><pre>``
`解法二：Floyd算法`
```#include<set>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Inf 1<<29
using namespace std;
int main()
{
int n,m;
int mp[100][100];
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
for(int j=1; j<=n; j++)
mp[i][j] = (i==j? 0 : Inf);
for(int i=0; i<m; i++)
{
int a,b,x;
scanf("%d %d %d",&a,&b,&x);
if(x < mp[a][b])
mp[a][b] = mp[b][a] = x;
}
for(int k=0; k<n; k++)
{
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(i!=j && j!=k)
{
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
}
int s,t;
scanf("%d%d",&s,&t);
if(mp[s][t]==Inf)
printf("-1\n");
else
printf("%d\n",mp[s][t]);
}
return 0;
}
```
`解法三：Bellman-Ford（贝尔曼）算法`

#include<set> #include<queue> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define Inf 1<<29 using namespace std; int dis[206]; int vis[205]; struct node {     int u;     int v;     int w; } q[205*205]; int n,m; void bellman_floyd(int s) {     for(int i=0; i<=n; i++)     {         dis[i]=Inf;     }     dis[s]=0;     for(int i=1; i<=n; i++)     {         for(int j=1; j<=2*m+1; j++)         {             if(dis[q[j].u]+q[j].w<dis[q[j].v])                 dis[q[j].v]=dis[q[j].u]+q[j].w;         }     }     return ; } int main() {     while(~scanf(“%d%d”,&n,&m))     {         for(int i=1; i<=2*m; i+=2)         {             scanf(“%d%d%d”,&q[i].u, &q[i].v, &q[i].w);             q[i+1].v=q[i].u;             q[i+1].u=q[i].v;             q[i+1].w=q[i].w;         }         int t,w;         scanf(“%d%d”,&t,&w);         bellman_floyd(t);         if(dis[w]==Inf)             printf(“-1\n”);         else             printf(“%d\n”,dis[w]);         memset(q,0,sizeof(q));     }     return 0; } 解法四：SPFA

``````#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 205;
vector<pair<int ,int> >E[maxn];
int n,m;
int d[maxn],inq[maxn];
void init()
{
for(int i=0; i<maxn; i++) E[i].clear();
for(int i=0; i<maxn; i++)inq[i]=0;
for(int i=0; i<maxn; i++) d[i]=1e9;

}
int main()
{
while(cin>>n>>m)
{
init();
for(int i=0; i<m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
E[x].push_back(make_pair(y,z));
E[y].push_back(make_pair(x,z));
}
int s,t;
scanf("%d%d",&s,&t);
queue<int> Q;
Q.push(s),d[s]=0,inq[s]=1;
while(!Q.empty())
{
int now =Q.front();
Q.pop();
inq[now]=0;
for(int i=0; i<E[now].size(); i++)
{
int v = E[now][i].first;
if(d[v]>d[now]+E[now][i].second)
{
d[v]=d[now]+E[now][i].second;
if(inq[v]==1)
continue;
inq[v]=0;
Q.push(v);
}
}
}
if(d[t]==1e9)
printf("-1\n");
else
printf("%d\n",d[t]);
}
return 0;
}``````

```END！！！更新中。。。
```
原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/XLJ_XLJ/article/details/52291393
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。