最近准备练习一下最短路,详情见vjudge //[kuangbin带你飞]专题四 最短路练习
1.POJ 2387
Description
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
* 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
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 }
Description
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
Output
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
Description
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
题意:有n个城市,m条边,每条边都有一个最大承载量,求最多能运送多少东西从1-n。这题和上一题很有一点像,只不过这题是求1-n中的多种路径每条路径中的最小值的最大值。
思路:用dijkstra,一开始选点的时候按照平常选边最小的那个点,直接WA了,其实每次要选能承载最多的那个边,还有就是给临界矩阵赋初值的时候要全部赋0,这个时候代表这条路的承载量是0,也就是不能走,不同的题目算法的用法也不一样。
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <vector> 5 #include <algorithm> 6 #include <iostream> 7 #include <math.h> 8 #define INF 999999999 9 using namespace std; 10 int map[1010][1010]; 11 int dis[1010]; 12 int vis[1010]; 13 int t; 14 int n,m; 15 void init() 16 { 17 memset(vis,false,sizeof(vis)); 18 memset(map,0,sizeof(map)); 19 } 20 void dij() 21 { 22 for(int i=1;i<=n;i++) 23 { 24 dis[i]=map[1][i]; 25 } 26 for(int k=1;k<=n;k++) 27 { 28 int maxn=-1; 29 int x; 30 for(int i=1;i<=n;i++) 31 { 32 if(!vis[i]&&dis[i]>maxn) 33 { 34 maxn=dis[i]; 35 x=i; 36 } 37 } 38 vis[x]=true; 39 for(int i=1;i<=n;i++) 40 { 41 int minn; 42 minn=min(dis[x],map[x][i]); 43 dis[i]=max(dis[i],minn); 44 } 45 } 46 47 } 48 int main() 49 { 50 cin>>t; 51 for(int cnt=1;cnt<=t;cnt++) 52 { 53 scanf("%d %d",&n,&m); 54 init(); 55 for(int i=1;i<=n;i++) 56 map[i][i]=0; 57 for(int i=1;i<=m;i++) 58 { 59 int x,y,d; 60 scanf("%d %d %d",&x,&y,&d); 61 map[x][y]=map[y][x]=d; 62 } 63 64 dij(); 65 printf("Scenario #%d:\n%d\n",cnt,dis[n]); 66 if(cnt!=t) 67 cout<<endl; 68 69 } 70 }
4.POJ 3268
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
题意:有N个农场,M条单向边,给定一个编号为X的农场,每个农场都有一个奶牛,买个奶牛从自己的农场到编号为X的农场都有一条最短路,回来的话也有一条最短路,求其他N-1个农场的牛来回路径中的最长的一条
思路:刚开始脑残想着什么floyd,n^3的复杂度肯定是不行的,强行对每个点进行spfa,发现居然可以过,再想一下发现可以把边都反过来,比如W(1,2)=3,W(2,1)=4;
然后反向之后就可以直接W'(1,2)=4,W'(2,1)=3,这样只要求两次x的spfa就行了;
第一种思路:
| 3268 | Accepted | 4768K | 313MS | G++ | 1436B |