最短路练习
最近准备练习一下最短路,详情见vjudge //[kuangbin带你飞]专题四 最短路练习
1.POJ 2387
A - Til the Cows Come Home
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
题意:给你T条边,求从1到N的最短路径;
思路:SPFA,邻接表存图
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <stdlib.h> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 using namespace std; 9 int n,m; 10 struct node 11 { 12 int v; 13 int dis; 14 }; 15 vector<node> Map[3000]; 16 int dis[3000]; 17 bool vis[3000]; 18 void addedge(int from,int to,int dis) 19 { 20 node e; 21 e.v=to; 22 e.dis=dis; 23 Map[from].push_back(e); 24 } 25 void spfa() 26 { 27 queue<int> q; 28 memset(dis,0x3f,sizeof(dis)); 29 memset(vis,false,sizeof(vis)); 30 q.push(1); 31 vis[1]=true; 32 dis[1]=0; 33 while(!q.empty()) 34 { 35 int x; 36 x=q.front(); 37 q.pop(); 38 for(int i=0;i<Map[x].size();i++) 39 { 40 node p=Map[x][i]; 41 if(dis[p.v]>dis[x]+p.dis) 42 { 43 dis[p.v]=dis[x]+p.dis; 44 if(!vis[p.v]) 45 { 46 vis[p.v]=true; 47 q.push(p.v); 48 } 49 } 50 } 51 vis[x]=false; 52 } 53 return ; 54 } 55 int main() 56 { 57 while(scanf("%d %d",&n,&m)!=EOF) 58 { 59 for(int i=1;i<=n;i++) 60 { 61 int a,b,c; 62 scanf("%d %d %d",&a,&b,&c); 63 addedge(a,b,c); 64 addedge(b,a,c); 65 } 66 spfa(); 67 printf("%d\n",dis[m]); 68 } 69 return 0; 70 }
2.POJ 2253
B - Frogger
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题意:给你几个点的坐标,从第一个点到第二个点,有很多种路径,求其中的一条路径的最长边是其他路径中的最短的
思路1:用dijkstra来做,用dis[i]记录从1到这个点多种路径中的最长边的最短边,比如现在从i到j,dis[j]=min(dis[j],map[i][j]);
for(int j=1;j<=n;j++)
{
if(!vis[j])
{
maxn=max(dis[i],map[i][j]);//求出这个时候经过i到j的最大的边
dis[j]=min(dis[j],maxn);//如果这个时候已知这个最长边比最长中的最短边还要短就要更新一下
}
}
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <stdlib.h> 7 #include <cmath> 8 #include <queue> 9 #define INF 999999999 10 using namespace std; 11 struct node 12 { 13 int x,y; 14 15 }; 16 node a[300]; 17 double map[300][300]; 18 bool vis[300]; 19 double dis[300]; 20 int cnt; 21 int n; 22 double cal(node a,node b) 23 { 24 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 25 } 26 void init() 27 { 28 memset(dis,0,sizeof(dis)); 29 memset(vis,false,sizeof(vis)); 30 31 } 32 void dij() 33 { 34 init(); 35 vis[1]=true; 36 dis[1]=0; 37 for(int i=1;i<=n;i++) 38 dis[i]=map[1][i]; 39 for(int i=1;i<=n;i++) 40 { 41 double minn=INF; 42 int x; 43 for(int j=1;j<=n;j++) 44 { 45 if(!vis[j]&&dis[j]<=minn) 46 { 47 minn=dis[j]; 48 x=j; 49 } 50 } 51 vis[x]=true; 52 for(int j=1;j<=n;j++) 53 { 54 if(!vis[j]) 55 { 56 double maxn=max(dis[x],map[x][j]); 57 if(dis[j]>maxn) 58 dis[j]=maxn; 59 60 } 61 } 62 } 63 return ; 64 } 65 66 int main() 67 { 68 cnt=0; 69 while(scanf("%d",&n)&&n) 70 { 71 cnt++; 72 for(int i=1;i<=n;i++) 73 { 74 scanf("%d %d",&a[i].x,&a[i].y); 75 76 } 77 for(int i=1;i<=n;i++) 78 { 79 for(int j=1;j<=n;j++) 80 { 81 if(i==j) continue; 82 else map[i][j]=cal(a[i],a[j]); 83 } 84 } 85 dij(); 86 printf("Scenario #%d\n",cnt); 87 printf("Frog Distance = %.3f\n\n",dis[2]); 88 } 89 return 0; 90 }
思路2:用floyd来做,,map[i][j],代表从i-j中多条路径中最长边的最小值,每次枚举一个点k,从i-j可以从i-k-j或者i-j,当i-k,和k-j,的权值都要比i-j要小的话
肯定是走i-k-j的,那么由于是求最长边,所以map[i][j]=max(map[i][k],map[k][j]);
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <stdlib.h> 7 #include <cmath> 8 #include <queue> 9 #define INF 0x3f 10 using namespace std; 11 struct node 12 { 13 int x,y; 14 15 }; 16 node a[300]; 17 double map[300][300]; 18 int cnt; 19 int n; 20 double cal(node a,node b) 21 { 22 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 23 } 24 void floyd() 25 { 26 for(int k=1;k<=n;k++) 27 { 28 for(int i=1;i<=n;i++) 29 { 30 for(int j=1;j<=n;j++) 31 { 32 if(map[i][k]<=map[i][j]&&map[k][j]<=map[i][j]) 33 { 34 map[i][j]=max(map[i][k],map[k][j]); 35 } 36 37 } 38 } 39 } 40 41 } 42 int main() 43 { 44 cnt=0; 45 while(scanf("%d",&n)&&n) 46 { 47 cnt++; 48 for(int i=1;i<=n;i++) 49 { 50 scanf("%d %d",&a[i].x,&a[i].y); 51 52 } 53 for(int i=1;i<=n;i++) 54 { 55 for(int j=1;j<=n;j++) 56 { 57 if(i==j) continue; 58 else map[i][j]=cal(a[i],a[j]); 59 } 60 } 61 floyd(); 62 printf("Scenario #%d\n",cnt); 63 printf("Frog Distance = %.3f\n\n",map[1][2]); 64 } 65 return 0; 66 }
3.POJ 1793
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 #define N 1010 9 #define INF 999999999 10 struct node 11 { 12 int to; 13 int cost; 14 }; 15 vector<node> G[N]; 16 int dis[N][N]; 17 int ans[N*N]; 18 int n,m,x; 19 bool vis[N]; 20 void addedge(int from,int to,int cost) 21 { 22 node e; 23 e.to=to; 24 e.cost=cost; 25 G[from].push_back(e); 26 } 27 void spfa(int y) 28 { 29 memset(vis,false,sizeof(vis)); 30 31 for(int i=1;i<=n;i++) 32 dis[y][i]=INF; 33 dis[y][y]=0; 34 queue<int> q; 35 q.push(y); 36 vis[y]=true; 37 while(!q.empty()) 38 { 39 int v; 40 v=q.front(); 41 q.pop(); 42 for(int i=0;i<G[v].size();i++) 43 { 44 node p=G[v][i]; 45 if(dis[y][p.to]>dis[y][v]+p.cost) 46 { 47 dis[y][p.to]=dis[y][v]+p.cost; 48 if(!vis[p.to]) 49 { 50 vis[p.to]=true; 51 q.push(p.to); 52 } 53 } 54 } 55 vis[v]=false; 56 } 57 } 58 int main() 59 { 60 while(cin>>n>>m>>x) 61 { 62 for(int i=1;i<=m;i++) 63 { 64 int a,b,c; 65 scanf("%d %d %d",&a,&b,&c); 66 addedge(a,b,c); 67 } 68 for(int i=1;i<=n;i++) 69 spfa(i); 70 for(int i=1;i<=n;i++) 71 { 72 ans[i]=dis[i][x]+dis[x][i]; 73 } 74 sort(ans+1,ans+1+n); 75 printf("%d\n",ans[n]); 76 } 77 return 0; 78 }
第二种思路:
3268 | Accepted | 968K | 63MS | G++ | 1558B |
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 #define N 1010 9 #define INF 999999999 10 struct node 11 { 12 int to; 13 int cost; 14 }; 15 vector<node> G1[N]; 16 vector<node> G2[N]; 17 int dis1[N]; 18 int dis2[N]; 19 int ans[N*N]; 20 int n,m,x; 21 bool vis[N]; 22 void addedge(int from,int to,int cost,int flag) 23 { 24 node e; 25 e.to=to; 26 e.cost=cost; 27 if(flag==1) 28 G1[from].push_back(e); 29 else 30 G2[from].push_back(e); 31 } 32 void spfa(vector<node>G[N],int dis[],int len) 33 { 34 35 memset(vis,false,sizeof(vis)); 36 memset(dis,0x3f,len); 37 dis[x]=0; 38 queue<int> q; 39 q.push(x); 40 vis[x]=true; 41 while(!q.empty()) 42 { 43 int v; 44 v=q.front(); 45 q.pop(); 46 for(int i=0;i<G[v].size();i++) 47 { 48 node p=G[v][i]; 49 if(dis[p.to]>dis[v]+p.cost) 50 { 51 dis[p.to]=dis[v]+p.cost; 52 if(!vis[p.to]) 53 { 54 vis[p.to]=true; 55 q.push(p.to); 56 } 57 } 58 } 59 vis[v]=false; 60 } 61 } 62 int main() 63 { 64 while(cin>>n>>m>>x) 65 { 66 for(int i=1;i<=m;i++) 67 { 68 int a,b,c; 69 scanf("%d %d %d",&a,&b,&c); 70 addedge(a,b,c,1); 71 addedge(b,a,c,2); 72 } 73 spfa(G1,dis1,sizeof(dis1)); 74 spfa(G2,dis2,sizeof(dis2)); 75 for(int i=1;i<=n;i++) 76 ans[i]=dis1[i]+dis2[i]; 77 sort(ans+1,ans+1+n); 78 printf("%d\n",ans[n]); 79 } 80 return 0; 81 }