| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
wa了不知多少次,终于用spfa给ac了,wa的同学多看看讨论,附上ac代码 SPFA版//poj2387
//diskstra或者spfa
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
typedef struct
{
int x;
int y;
}type;
bool vis[1010];
#define max 99999999;
int main()
{
int n,m,i,a,b,c;
int d[1010];
type node;
vector<type> g[1010];
queue<int> q;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
node.x=b;
node.y=c;
g[a].push_back(node);
node.x=a;
g[b].push_back(node);
}
for(i=1;i<=n;i++)
{
d[i]=max;
}
memset(vis,0,sizeof(vis));
d[1]=0;
q.push(1);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false; //清除在队列里的标志
for(i=0;i<g[u].size();i++)
{
int v=g[u][i].x;
int w=g[u][i].y;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
printf("%d\n",d[n]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator