poj Til the Cows Come Home(dijkstra,bellman_ford,spfa)
来源:互联网 发布:淘宝植美村旗舰店 编辑:程序博客网 时间:2024/05/16 10:21
Til the Cows Come Home
Description
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 51 2 202 3 303 4 204 5 201 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
dijkstra代码:
#include<stdio.h>#include<string.h>#define N 1000+10const int INF=0x3f3f3f3f;int map[N][N];int n,m;void dijkstra(){ int i,j,min,v; int dis[N]; bool vis[N]; for(i=1;i<=n;i++)//初始化起点到其余各个顶点的路程并且初始化vis数组 { vis[i]=0; dis[i]=map[1][i]; } vis[1]=1; for(i=1;i<=n-1;i++) { min=INF; for(j=1;j<=n;j++)//找到离起点最近的顶点 { if(!vis[j]&&dis[j]<min) { min=dis[j]; v=j; } } vis[v]=1; for(j=1;j<=n;j++)//松弛 { if(!vis[j]&&dis[j]>map[v][j]+dis[v]) dis[j]=map[v][j]+dis[v]; } } printf("%d\n",dis[n]);}int main(){ while(~scanf("%d%d",&m,&n)) { int i,j; for(i=1; i<=n; i++)//初始化 for(j=1; j<=n; j++) if(i==j) map[i][j]=0; else map[i][j]=map[j][i]=INF; int x,y,z; for(i=1;i<=m;i++)//邻接矩阵存图 { scanf("%d%d%d",&x,&y,&z); if(map[x][y]>z) map[x][y]=map[y][x]=z; } dijkstra(); } return 0;}
bellman_ford代码:
#include<stdio.h>#define maxn 2005#define maxv 1005const int inf=0x3f3f3f3f;typedef struct{ int u,v,w;} Egde;Egde egde[maxn];int n,m;void bell_man_ford(){ int i,j; int d[maxv]; for(i=2; i<=n; i++)//初始化 d[i]=inf; d[1]=0; for(i=1; i<=n-1; i++)//迭代n-1次 { for(j=1; j<=m; j++)//检查每条边 { if(d[egde[j].u]>d[egde[j].v]+egde[j].w) d[egde[j].u]=d[egde[j].v]+egde[j].w; if(d[egde[j].v]>d[egde[j].u]+egde[j].w) d[egde[j].v]=d[egde[j].u]+egde[j].w; } } printf("%d\n",d[n]);}int main(){ int x,y,z,i; while(~scanf("%d%d",&m,&n)) { for(i=1; i<=m; i++) { scanf("%d%d%d",&x,&y,&z); egde[i].u=x; egde[i].v=y; egde[i].w=z; } bell_man_ford(); } return 0;}
spfa代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define maxn 4000+10const int inf=0x3f3f3f3f;struct node{ int u,v,w;} G[maxn];int n,m,len;int first[maxn],next[maxn];int d[maxn];bool vis[maxn];void spfa(int star){ int i,j; for(i=1; i<=n; i++)//初始化到起点的距离 { d[i]=inf; vis[i]=0; } d[star]=0; vis[star]=1;//初始时只有起点在队列中 queue<int>q; q.push(star); while(!q.empty()) { star=q.front(); q.pop(); vis[star]=0;//清除“在队列中“的标志 for(i=first[star]; i!=-1; i=next[i]) { if(d[G[i].v]>d[star]+G[i].w) { d[G[i].v]=d[star]+G[i].w; if(!vis[G[i].v]) { vis[G[i].v]=1; q.push(G[i].v); } } } } printf("%d\n",d[n]);}void add_egde(int u,int v,int w)//邻接表存图{ G[len].u=u,G[len].v=v,G[len].w=w; next[len]=first[u]; first[u]=len++;}int main(){ int x,y,z,i; while(~scanf("%d%d",&m,&n)) { memset(first,-1,sizeof(first));//初始化first数组,表示1~n顶点暂时都没有边 len=1; for(i=1; i<=m; i++) { scanf("%d%d%d",&x,&y,&z); add_egde(x,y,z); add_egde(y,x,z); } spfa(1); } return 0;}
ps:开始进军最短路!
1 0
- poj Til the Cows Come Home(dijkstra,bellman_ford,spfa)
- poj 2387 Til the Cows Come Home(最短路SPFA+Dijkstra)
- POJ 2387 Til the Cows Come Home(spfa)
- POJ 2387 Til the Cows Come Home(Spfa)
- POJ-2387-Til the Cows Come Home(SPFA)
- POJ 2387 Til the Cows Come Home(Dijkstra判重边)
- POJ 2387 Til the Cows Come Home(Dijkstra)
- poj 2387 Til the Cows Come Home(Dijkstra)
- POJ - 2387 Til the Cows Come Home(Dijkstra)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- poj Til the Cows Come Home (Dijkstra最短路~)
- Til the Cows Come Home (Dijkstra)
- POJ2387 Til the Cows Come Home(dijkstra + heap 或 SPFA)
- POJ 2387 Til the Cows Come Home (Spfa)
- poj 2387 Til the Cows Come Home spfa+dis解答
- POJ 2387 Til the Cows Come Home (Spfa) .
- POJ 2387 Til the Cows Come Home (裸SPFA)
- 集合框架_集合多层嵌套的数据分析
- 降低make版本
- 17 - 04 - 09 Web安全(05)
- java nio: Files.isWritable判断文件夹可写(创建子文件夹或文件)
- def定义函数
- poj Til the Cows Come Home(dijkstra,bellman_ford,spfa)
- 2016年转行做java
- javascript散列
- 进程, 线程的定义与区别
- php单例模式(转)
- Git版本控制工具使用教程(二)
- 心灵终结(找规律)
- 电脑系统重装出现蓝屏解决方法
- 关于java线程安全问题