您的位置:首页 > 其它

POJ 2387 Til the Cows Come Home

2014-12-15 10:01 375 查看
题目大意:

给你N个点 T条边, 求N->1的最短路

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1002

struct Edge
{
int e, w;
};

vector<Edge> G[1002];
bool vis[maxn];
int dist[maxn], n, m;;

void Init()
{
for(int i=0; i<=n; i++)
{
G[i].clear();
dist[i] = INF;
vis[i] = false;
}
}
int Spfa()
{
Edge P, Pn;
queue<Edge> Q;
P.e = n, P.w = 0;
dist
= 0;
Q.push(P);

while( !Q.empty() )
{
P = Q.front();
Q.pop();

vis[P.e] = false;
int len = G[P.e].size();

for(int i=0; i<len; i++)
{
Pn = G[P.e][i];

if(dist[Pn.e] > dist[P.e] + Pn.w)
{
dist[Pn.e] = dist[P.e] + Pn.w;

if( !vis[Pn.e] )
{
Q.push(Pn);
vis[Pn.e] = true;
}
}
}
}
return dist[1];
}
int main()
{
Edge P;

while(cin >> m >> n)
{
Init();
for(int i=0; i<m; i++)
{
int a, b ,c;

scanf("%d%d%d",&a,&b,&c);

P.e = b, P.w = c;
G[a].push_back(P);
P.e = a;
G[b].push_back(P);
}

int ans = Spfa();

cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: