您的位置:首页 > 其它

|Vijos|图论最短路|P1391 想越狱的小杉

2016-08-20 22:55 381 查看
http://vijos.org/p/1391

SPFA,感觉不是真正意义上的SPFA。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ms(i,j) memset(i, j, sizeof(i));
using namespace std;
const int maxn = 2000 + 5;
int w[maxn][maxn];
int edge[maxn][maxn];
int dis[maxn];
int ex[maxn];
queue<int> q;
int n;
int main ()
{
scanf("%d", &n);
int a,b,c;
for (int i=1;i<=n;i++)for (int j=1;j<=n;j++) w[i][j]=100000000;
while (scanf("%d%d%d", &a, &b, &c)==3&&(a||b||c))
{
w[a][b]=c;
}
ms(dis,0);ms(ex,false);
dis[1] = 100000000;ex[1]=true;
q.push(1);
while(!q.empty())
{
int u = q.front();q.pop();ex[u]=false;
for (int i=1;i<=n;i++)
{
if (w[u][i]!=100000000&&dis[i]<min(dis[u], w[u][i]))
{
dis[i]=min(dis[u], w[u][i]);
if (!ex[i])
{
ex[i]=true;
q.push(i);
}
}
}
}
for (int i=2;i<=n;i++) printf("%d\n", dis[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: