Til the Cows Come Home(赶牛回家)
2011-08-18 13:33
302 查看
poj 2387
题目大意:求出图中最短的路径
解决:迪杰斯特拉 或者 spfa 在此处用spfa(队列)
题目大意:求出图中最短的路径
解决:迪杰斯特拉 或者 spfa 在此处用spfa(队列)
#include <iostream> #include <cstdio> #include <queue> using namespace std; const int N=1005; const int MAX=0x3f3f3f3f; int cost ; int dist ; bool inq ; int n; void init(int v0) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cost[i][j]=MAX; dist[i]=MAX; inq[i]=false; } dist[v0]=0; } void spfa(int v0) {//spfa实现部分 queue<int> q; q.push(v0); inq[v0]=1; while(!q.empty()) { v0=q.front(); q.pop(); inq[v0]=0; for(int i=1;i<=n;i++) if(cost[v0][i]<MAX && dist[v0]+cost[v0][i]<dist[i]) { //有了这个判断,让时间从160ms 到 30ms,一定要判断,否则进队列 的就多了,可以多次进,但不是在队列一个了,还要进 dist[i]=dist[v0]+cost[v0][i]; if(!inq[i]){ inq[i]=1; q.push(i);} } } } int main() { int t; scanf("%d%d",&t,&n); int a,b,w; init(1); for(int i=0;i<t;i++) { scanf("%d%d%d",&a,&b,&w); if(cost[a][b]>w)cost[b][a]=cost[a][b]=w; } spfa(1); printf("%d\n",dist ); system("pause"); return 0; }
相关文章推荐
- [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- [Poj 2387][Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- bzoj 3386: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- Til the Cows Come Home 【最短路】
- poj——2387——Til the Cows Come Home(简单最短路)
- poj2387 - Til the Cows Come Home(最短路径问题) (Dijkstra)
- 第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- Til the Cows Come Home POJ - 2387 dijkstra
- POJ 2387 Til the Cows Come Home(Dijkstra判重边)
- (简单) POJ 2387 Til the Cows Come Home,Dijkstra。
- POj2387——Til the Cows Come Home——————【最短路】
- poj Til the Cows Come Home (Dijkstra最短路~)
- POJ2387---Til the Cows Come Home (最短路模板)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ2387 Til the Cows Come Home(dijkstra)
- Til the Cows Come Home POJ - 2387 单源最短路 SPFA实现
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)