Dijkstra的变形 - Heavy Transportation、Frogger

变形一:

求每条路径中边的最大值,然后求所有路径的边的最小值的最大值。

变形二:

求每条路径中边的最大值,然后求所有路径的边的最大值的最小值。

理解题意是关键啊……

注意:cost数组的初始值不一样啊啊啊,求边最小值的最大值,设初始值为0

求边最大值的最小值,设初始值为1。

变形一的题:

Heavy Transportation


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

分析:

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).

给出两个地点的承重限制,求1到n的所能承受的最大重量,比如样例1->2=3,1->3=4,2->3=5;

则1->3可以是:1->2->3---最大承重3(min(3,5))

         1->3---最大承重4

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define INF 9999999
int n,cost[1005][1005],d[1005],used[1005];
using namespace std;

void Dijkstra(int s){
    for(int i=1;i<=n;i++){
        d[i]=cost[s][i];
    }
    memset(used,0,sizeof(used));
    d[s]=0;

    //d[i]表示点i到原点的每天路径上最小边的最大值
    for(int k=1;k<=n;k++){
        int mymax=-1,v=-1;
        for(int i=1;i<=n;i++){
            if(!used[i]&&mymax<d[i]){mymax=d[i];v=i;}
        }
        
        used[v]=1;
        for(int i=1;i<=n;i++){
            d[i]=max(d[i],min(d[v],cost[v][i]));
        }
    }
}
int main(){
    int f,a,b,c,m;
    scanf("%d",&f);
    for(int q=1;q<=f;q++){
        scanf("%d%d",&n,&m);
        memset(cost,0,sizeof(cost));
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            cost[a][b]=cost[b][a]=c;
        }
        Dijkstra(1);
        printf("Scenario #%d:\n%d\n\n",q,d[n]);
    }
}

变形二的题:

Frogger

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

分析:

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. 

蛙A在i石头上,蛙B在2石头上,中间有许多中介的石头

青蛙跳(frog's jump):是在一条路径上跳的最长的距离。

青蛙距离(frog distance):是最小的青蛙跳距离。

求青蛙距离,也就是各条路径的最长边的最小值

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 99999999
using namespace std;
int n;
int used[203];
double d[203],cost[203][203];
pair<int,int>p[203];

double getd(pair<int,int>p1,pair<int,int>p2){
    return sqrt((double)(p1.first-p2.first)*(p1.first-p2.first)+(p1.second-p2.second)*(p1.second-p2.second));
}

void dijkstra(int s){
    for(int i=1;i<=n;i++){
        d[i]=cost[1][i];
    }
    memset(used,0,sizeof(used));
    d[s]=0;

    for(int k=1;k<=n;k++){//循环n次
        int v=-1;
        double mymin=INF;
        for(int i=1;i<=n;i++){
            if(!used[i]&&mymin>=d[i]){mymin=d[i];v=i;}
        }
        used[v]=1;
        for(int i=1;i<=n;i++){
            d[i]=min(d[i],max(d[v],cost[v][i]));
        }
    }

}

int main(){
    int cas=1;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        int x,y;
        for(int i=1;i<=n;i++){//算出来每个点到第一个点的距离
            scanf("%d%d",&x,&y);
            p[i].first=x;p[i].second=y;
        }

        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                if(i==j)cost[i][j]=0;
                else cost[i][j]=cost[j][i]=getd(p[i],p[j]);
            }
        }
        dijkstra(1);
        printf("Scenario #%d\n",cas++);
        printf("Frog Distance = %.3f\n\n",d[2]);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37579232/article/details/80113662