寒假刷题-最短路

来源:互联网 发布:王力宏 乐器 知乎 编辑:程序博客网 时间:2024/05/17 06:59
A - Til the Cows Come Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
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.










B - Frogger
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
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. 
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












C - Heavy Transportation
Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Background 
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
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4












D - Silver Cow Party
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
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
Line 1: Three space-separated integers, respectively: N, M, and X
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
Line 1: One integer: the maximum of time any one cow must walk.
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

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.







代码:

A:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int lu[1050][1050];
int main()
{
int n,t;
scanf("%d%d",&t,&n);
memset(lu,0x3f3f3f,sizeof(lu));
int a,b,c;
for (int i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
lu[a][b]=lu[b][a]=min(lu[a][b],c);
}
int dis[1050];
bool fafe[1050];
memset(fafe,true,sizeof(fafe));
for (int i=1;i<=n;i++)
dis[i]=lu[1][i];
dis[1]=0;
fafe[1]=false;
int p,mi;
for (int i=1;i<n;i++)
{
mi=0x3f3f3f;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis[j]<mi)
{
mi=dis[j];
p=j;
}
}
if (p==n)
break;
fafe[p]=false;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis[j]>dis[p]+lu[p][j])
dis[j]=dis[p]+lu[p][j];
}
}
printf("%d\n",dis[n]);
return 0;
}



B:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct nood{
int aa,bb;
double cc;
}lu[220*100];
bool cmp(nood xx,nood yy){
return xx.cc<yy.cc;
}
int fer[220];
int find(int xx)
{
while (fer[xx]!=xx)
xx=fer[xx];
return xx;
}
int main()
{
    int n,t=0;
double x[220],y[220];
while (~scanf("%d",&n))
{
if (n==0) break;
t++;
for (int i=1;i<=n;i++)
   scanf("%lf%lf",&x[i],&y[i]);
   int p=0;
   for (int i=1;i<=n;i++)
   for (int j=i+1;j<=n;j++)
{
lu[p].aa=i;lu[p].bb=j;
lu[p].cc=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
p++;
}
sort(lu,lu+p,cmp);
double s;
for (int i=1;i<=n;i++)
fer[i]=i;
for (int i=0;i<p;i++)
{
fer[find(lu[i].aa)]=find(lu[i].bb);
if (find(1)==find(2))
{
//printf("%.3lf",lu[i].cc);测试数据忘//了,一直w....... 
s=lu[i].cc;
break;
}
}
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",t,s);
}
return 0;
}



C:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct nood{
int aa,bb,cc;
}lu[1000*550];
bool cmp(nood xx,nood yy){
return xx.cc>yy.cc;
}
int fer[1050];
int find(int xx)
{
while (fer[xx]!=xx)
xx=fer[xx];
return xx;
}
int main()
{
    int t;scanf("%d",&t);
for (int kk=1;kk<=t;kk++)
{
int n,m;
scanf("%d%d",&n,&m);
   int a,b,c;
   for (int i=0;i<m;i++)
scanf("%d%d%d",&lu[i].aa,&lu[i].bb,&lu[i].cc);
sort(lu,lu+m,cmp);
for (int i=1;i<=n;i++)
fer[i]=i;
int ss=0;
for (int i=0;i<m;i++)
{
   fer[find(lu[i].aa)]=find(lu[i].bb);
if (find(1)==find(n))
{
ss=lu[i].cc;
break;
}
}
printf("Scenario #%d:\n%d\n\n",kk,ss); 
}
return 0;
}


D:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int lai[1050][1050];
int hui[1050][1050];
int dis[1050];
int dis1[1050];
int dis2[1050];
int n,m,x;
void prim1()
{
bool fafe[1050];
memset(fafe,true,sizeof(fafe));
for (int i=1;i<=n;i++)
dis1[i]=lai[x][i];
dis1[x]=0;fafe[x]=false;
int p,mi;
for (int i=1;i<n;i++)
{
mi=0x3f3f3f;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis1[j]<mi)
{
mi=dis1[j];
p=j;
}
}
fafe[p]=false;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis1[j]>dis1[p]+lai[p][j])
{
dis1[j]=dis1[p]+lai[p][j];
}
}
}
}
void prim2()
{
bool fafe[1050];
memset(fafe,true,sizeof(fafe));
for (int i=1;i<=n;i++)
dis2[i]=hui[x][i];
dis2[x]=0;fafe[x]=false;
int p,mi;
for (int i=1;i<n;i++)
{
mi=0x3f3f3f;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis2[j]<mi)
{
mi=dis2[j];
p=j;
}
}
fafe[p]=false;
for (int j=1;j<=n;j++)
{
if (fafe[j]&&dis2[j]>dis2[p]+hui[p][j])
{
dis2[j]=dis2[p]+hui[p][j];
}
}
}
}
int main()
{


scanf("%d%d%d",&n,&m,&x);
memset(lai,0x3f3f3f,sizeof(lai));
memset(hui,0x3f3f3f,sizeof(hui));
int a,b,c;
for (int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
lai[a][b]=hui[b][a]=c;
}
prim1();
prim2();
for (int i=1;i<=n;i++)
dis[i-1]=dis1[i]+dis2[i];
sort(dis,dis+n);
printf("%d\n",dis[n-1]);
return 0;
}

0 0