C - Heavy Transportation && B - Frogger(迪杰斯变形)

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

思路:
这一道题就是说假如从a到b的路有好几条,在每条路上都要过几个路口,路口与路口之间的有一个标量的意思就是过这条路的最大质量是多少。。那就是说
求出来这一条路上面的最小值,只有小于等于这个值的货物才能通过这条路到达终点。。
但是要注意从a到b的路有可能不止一条,所以我们就要去求所有能到达终点每条路的最小值,再在最小值中取最大值
解决这道题的方法:选择迪杰斯方法的变形,具体实现就是给那个记录单源最短路长度的数组全部赋值为0,再把起点的距离设为无穷大,放入优先队列中
在每一次的判断d[终点]<min(d[起点],从起点到终点边的距离)
还要注意的是在使用优先队列的优先也发生了变化,我们是求每一条路上边的最小值,最后在所有的情况中取最大值
所以说我们要求的是最大值,这就和最短路不一样了,因此我们要改变优先级
代码如下:

 

 1 //终于A了。。。
 2 //原来这一道题优先队列的优先也和最短路的不一样,因为这是要求出来每一条路的最小边,
 3 //在在众多边进行对比找出来那个最大的。那么在刚开始对与七点相连的边进行一次遍历之后
 4 //就要找出来d中最大的值,再从他开始遍历。。。。
 5 #include<stdio.h>
 6 #include<string.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 #include<vector>
10 #include<queue>
11 using namespace std;
12 const int MAX=1005;
13 const int INF=0xffffff;
14 int n,m,d[MAX],dis[MAX];
15 struct shudui1
16 {
17     int start,value;
18     bool operator <(const shudui1 q)const
19     {
20         return value<q.value;
21     }
22 }str1;
23 struct shudui2
24 {
25     int start,value;
26 }str2;
27 priority_queue<shudui1>r;
28 vector<shudui2>w[MAX];
29 void JK()
30 {
31     memset(dis,0,sizeof(dis));
32     while(!r.empty())
33     {
34         str1=r.top();
35         r.pop();
36         int x=str1.start;
37         if(dis[x]) continue;
38         dis[x]=1;
39         int len=w[x].size();
40         for(int i=0;i<len;++i)
41         {
42             str2=w[x][i];
43             if(!dis[str2.start] && d[str2.start]<min(d[x],str2.value))
44             {
45                 str1.value=d[str2.start]=min(d[x],str2.value);
46                 str1.start=str2.start;
47                 r.push(str1);
48             }
49         }
50     }
51 }
52 int main()
53 {
54     int t,k=0;
55     scanf("%d",&t);
56     while(t--)
57     {
58         k++;
59         scanf("%d%d",&n,&m);
60         for(int i=1;i<=n;++i)
61         {
62             w[i].clear();
63         }
64         memset(d,0,sizeof(d));
65         while(m--)
66         {
67             int x,y,z;
68             scanf("%d%d%d",&x,&y,&z);
69             str2.start=y;
70             str2.value=z;
71             w[x].push_back(str2);
72             str2.start=x;
73             w[y].push_back(str2);
74         }
75         d[1]=INF;
76         str1.start=1;
77         str1.value=INF;
78         r.push(str1);
79         JK();
80 
81         printf("Scenario #%d:\n",k);
82         printf("%d\n\n",d[n]);
83     }
84     return 0;
85 }
View Code
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

这一道题与上面那一道题刚好相反
这一道题就是求出来从起点到终点的每一条路上面的边的最大值,和上一个差不多,这个也有好几条路,但是这个要在所有路中求最小值(花里胡哨)
这个对前期数组处理要把数组初始化为无穷大,那个起点是初始化为0
其他按照正常最短路就可以过
代码如下:

  1 //这一道题难受死我了,这个问题我也是醉了。。。
  2 //题意:
  3 //青蛙一是第一个输入的数据
  4 //青蛙而是第二个
  5 //由于从青蛙一到青蛙二的路有好几条,青蛙一也可以直接蹦到青蛙二的位置
  6 //所以要求这几条路中他们各自的蹦跳的最大值
  7 //在在这几条路中的最大值中求最小值。。。。。<_>
  8 #include<stdio.h>
  9 #include<string.h>
 10 #include<algorithm>
 11 #include<math.h>
 12 #include<iostream>
 13 #include<vector>
 14 #include<queue>
 15 using namespace std;
 16 struct shudui1
 17 {
 18     int start;
 19     double value;
 20     bool operator < (const shudui1 e)const
 21     {
 22         return value>e.value;
 23     }
 24 }str1;
 25 struct shudui2
 26 {
 27     int start;
 28     double value;
 29 }str2;
 30 struct shudui3
 31 {
 32     double x,y;
 33 }m[205];
 34 vector<shudui2>w[205];
 35 priority_queue<shudui1>r;
 36 const double INF=0xffffff;
 37 double v[205];
 38 int dis[205];
 39 int a,s,d,k=0;
 40 void JK()
 41 {
 42     //vis[1]=0;
 43     while(!r.empty())
 44     {
 45         str1=r.top();
 46         r.pop();
 47         int x=str1.start;
 48         double y=str1.value;
 49 //        if(v[x]<y)
 50 //        {
 51 //           // printf("****\n");
 52 //            continue;
 53 //        }
 54         if(dis[x]) continue;
 55         dis[x]=1;
 56         int len=w[x].size();
 57         //printf("%d %d \n",len,str1.start);
 58         for(int i=0;i<len;++i)
 59         {
 60             str2=w[x][i];
 61           //  printf("%d %d %d %d\n",v[str2.start],v[x],str2.value,str2.start);
 62             if(v[str2.start]>max(v[x],str2.value)) // 做题方法大致不变,但是v中存的值要改变,假比
 63 //                v[2]中原来值为2-------是青蛙一直接蹦了过去
 64 //                但是从青蛙一蹦到三号点距离为1.414,再从三号点蹦到二号点2--3--->距离:1.414
 65 //                此时大都青蛙二的路有两条
 66 //                1--->2;
 67 //                1--->3---->2,三中存的是一到三的最大值,到二的时候比较的时侯,v[3]就代表之前所有者一条路上的最大边,
 68 //                此时他的value是三道二这条边的长度,这样就相当于二中存的是1到2这条路上的边的最大值
 69 //            之后赋值给二的时候,如果二中有值,就代表这是其他路到二位值的最大值,再次赋值时要比较
 70             {
 71                // printf("******\n");
 72                 v[str2.start]=max(v[x],str2.value);
 73                 //v[str2.start]=v[x]+str2.value;
 74                 str1.start=str2.start;
 75                 str1.value=v[str2.start];
 76                 r.push(str1);
 77             }
 78         }
 79     }
 80 }
 81 int main()
 82 {
 83     while(~scanf("%d",&a))
 84     {
 85         k++;
 86         if(a==0) break;
 87         memset(dis,0,sizeof(dis));
 88         //memset(vis,0x3f,sizeof(vis));
 89 //        for(int i=1;i<=a;++i)
 90 //        {
 91 //            vis[i]=INF;
 92 //        }
 93         for(int i=1;i<=a;++i)
 94             v[i]=INF;
 95         for(int i=1;i<=a;++i)
 96         {
 97             scanf("%lf%lf",&m[i].x,&m[i].y);
 98         }
 99         double q;
100         for(int i=1;i<a;++i)
101         {
102             for(int j=i+1;j<=a;++j)
103             {
104                 //if((i==1 && j==2) || (i==2 && j==1)) continue;
105                 //if(i==j) continue;
106                 q=sqrt((m[i].x-m[j].x)*(m[i].x-m[j].x)+(m[i].y-m[j].y)*(m[i].y-m[j].y));
107                 str2.start=j;
108                 str2.value=q;
109                 w[i].push_back(str2);
110                 str2.start=i;
111                 w[j].push_back(str2);
112                 //printf("%d %d %lf\n",i,j,q);
113             }
114         }
115        // printf("%d %d\n",w[1][0].start,w[1].size());
116         v[1]=0;
117         str1.start=1;
118         str1.value=0;
119         r.push(str1);
120         JK();
121         printf("Scenario #%d\n",k);
122         printf("Frog Distance = %.3lf\n",v[2]);
123         //r.clear();
124         for(int i=1;i<=a;++i)
125         w[i].clear();
126         printf("\n");
127     }
128     return 0;
129 }
View Code

 




posted @ 2019-04-12 12:51  kongbursi  阅读(164)  评论(0编辑  收藏  举报