kuangbin 最短路练习 A题
2017-09-08 18:14
155 查看
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.
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.
注意判重边,数据中会有重边,取比较短的
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.
题解:
wa了好几次。原因是T组数据,不是N组数据。注意判重边,数据中会有重边,取比较短的
if(cost[a-1][b-1]>c) { cost[a-1][b-1]=c; cost[b-1][a-1]=c; }
代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1010; const int INF = 0x3f3f3f3f; bool vis[MAXN]; int pre[MAXN]; void Dijkstra(int cost[][MAXN],int lowcost[],int n,int beg) { for(int i=0;i<n;i++) { lowcost[i]=INF; vis[i]=false; pre[i]=-1; } lowcost[beg]=0; for(int j=0;j<n;j++) { int k=-1; int min=INF; for(int i=0;i<n;i++) { if(!vis[i]&&lowcost[i]<min) { min = lowcost[i]; k=i; } } if(k==-1) break; vis[k]=true; for(int i=0;i<n;i++) { if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) { lowcost[i]=lowcost[k]+cost[k][i]; pre[i]=k; } } } } int lowcost[MAXN]; int cost[MAXN][MAXN]; int main() { int T,N; while(cin>>T>>N) { int a,b,c; memset(cost,0x3f,sizeof(cost)); for(int i=0;i<T;i++) { cin>>a>>b>>c; if(cost[a-1][b-1]>c) { cost[a-1][b-1]=c; cost[b-1][a-1]=c; } } Dijkstra(cost,lowcost,N,0); cout<<lowcost[N-1]<<endl; } return 0; }
相关文章推荐
- [kuangbin带你飞]专题四 最短路练习 -A - Til the Cows Come Home
- [kuangbin带你飞]专题四 最短路练习 E POJ 1860
- [kuangbin带你飞]专题四 最短路练习 G POJ 1502
- [kuangbin带你飞]专题四 最短路练习 S POJ 3169
- [kuangbin带你飞]专题四 最短路练习
- [kuangbin带你飞]专题四 最短路练习-E
- [kuangbin带你飞]专题四 最短路练习
- [kuangbin带你飞]专题四 最短路练习 B
- [kuangbin带你飞]专题四 最短路练习 B
- [kuangbin带你飞]专题四 最短路练习 C
- [kuangbin带你飞]专题四 最短路练习-G
- [kuangbin带你飞]专题四 最短路练习 H POJ 3660
- [kuangbin带你飞]专题四 最短路练习 O LightOJ 1074
- [kuangbin带你飞]专题四 最短路练习 R HDU 437
- 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes
- [kuangbin带你飞]专题四 最短路练习 R
- [kuangbin带你飞]专题四 最短路练习 K POJ 3159
- [kuangbin带你飞]专题四 最短路练习 G MPI Maelstrom
- [kuangbin带你飞]专题四 最短路练习H,I,J
- [kuangbin带你飞]专题四 最短路练习