A - Til the Cows Come Home (dijkstra算法)
2018-01-22 16:11
253 查看
推荐Dijkstra算法讲解:http://blog.51cto.com/ahalei/1387799
A - Til the Cows Come Home
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.
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.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
Sample Output
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
WR 了好多发,终于过了;
注意: 判题时的数据, 可能会有 重复但用时比之前短的 路段;
列如: 前面有 1 4 5;后面又给了 1 4 2;那肯定需要 舍弃第一条数据,所以要在 输入时 加一个大小 判断;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 99999999
#define MM 1005
int e[MM][MM],dis[MM],book[MM];
int n,t,N1,N2,T,minn,flag;
int main()
{
scanf("%d%d",&t,&n);
for(int i=1; i<=n; i++) // 初始化 路程;
for(int j=1; j<=n; j++)
if(i==j) e[i][j]=0;
else e[i][j]=INF;
for(int i=0; i<t; i++)
{
scanf("%d %d %d",&N1,&N2,&T);
if(e[N1][N2]>T)
e[N1][N2]=e[N2][N1]=T; //路 是双向的;
}
memset(dis,0,sizeof(dis));
for(int i=1; i<=n; i++)
{
dis[i]=e[1][i]; // dijkstra 算法
book[i]=0;
}
book[1]=1;
for(int i=1; i<=n; i++)
{
minn=INF;
for(int j=1; j<=n; j++)
{
if(book[j]==0&&dis[j]<minn)
{
minn=dis[j];
flag=j;
}
}
book[flag]=1;
for(int v=1; v<=n; v++)
{
if(dis[v]>dis[flag]+e[flag][v])
dis[v]=dis[flag]+e[flag][v];
}
}
printf("%d\n",dis
);
return 0;
}
A - Til the Cows Come Home
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.
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.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 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.
WR 了好多发,终于过了;
注意: 判题时的数据, 可能会有 重复但用时比之前短的 路段;
列如: 前面有 1 4 5;后面又给了 1 4 2;那肯定需要 舍弃第一条数据,所以要在 输入时 加一个大小 判断;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 99999999
#define MM 1005
int e[MM][MM],dis[MM],book[MM];
int n,t,N1,N2,T,minn,flag;
int main()
{
scanf("%d%d",&t,&n);
for(int i=1; i<=n; i++) // 初始化 路程;
for(int j=1; j<=n; j++)
if(i==j) e[i][j]=0;
else e[i][j]=INF;
for(int i=0; i<t; i++)
{
scanf("%d %d %d",&N1,&N2,&T);
if(e[N1][N2]>T)
e[N1][N2]=e[N2][N1]=T; //路 是双向的;
}
memset(dis,0,sizeof(dis));
for(int i=1; i<=n; i++)
{
dis[i]=e[1][i]; // dijkstra 算法
book[i]=0;
}
book[1]=1;
for(int i=1; i<=n; i++)
{
minn=INF;
for(int j=1; j<=n; j++)
{
if(book[j]==0&&dis[j]<minn)
{
minn=dis[j];
flag=j;
}
}
book[flag]=1;
for(int v=1; v<=n; v++)
{
if(dis[v]>dis[flag]+e[flag][v])
dis[v]=dis[flag]+e[flag][v];
}
}
printf("%d\n",dis
);
return 0;
}
相关文章推荐
- Til the Cows Come Home(poj 2387 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算法)
- POJ2387 Til the Cows Come Home(最短路,Dijkstra算法,spfa算法,Floyd算法,深搜DFS)
- poj 2387 Til the Cows Come Home(dijkstra算法)
- POJ 2387 Til the Cows Come Home(最短路,Dijkstra算法)
- POJ-2387 Til the Cows Come Home(最短路 Dijkstra算法)
- Til the Cows Come Home(poj 2387 Dijkstra算法)
- POJ2387--Til the Cows Come Home(dijkstra算法)
- POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- Til the Cows Come Home(最短路—Dijkstra算法)
- (阶段三 dijkstra算法温习 1.6)POJ 2387 Til the Cows Come Home(使用dijkstra算法求单源起点和单源终点的最短路径)
- Til the Cows Come Home POJ - 2387 dijkstra
- Til the Cows Come Home(最短路,注意重边)
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
- POJ 2387 Til the Cows Come Home(简单的Dijkstra算法)
- Til the Cows Come Home POJ - 2387