• [kuangbin带你飞]专题四 最短路练习 A-D


      上了大二,又有新生来了,压力也来了,不能像大一这么松懈了,所以开始每天刷刷题,想根据专题一个知识点一个知识点过

      今天是最短路专题,对于最短路,能想到的方法大概就是dijkstra算法(求单源最短路不含负环)O(n^2)如果使用堆优化,就是O(mlogn),还有就是floyd算法(求图上任意两点的最短路)O(N^3),以及bellman算法(可求解含负环的单源最短路问题)

      对于a题 ,dijkstra算法

    A - Til the Cows Come Home

     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.

      求1-n的最短路,直接使用dij算法,要注意边的更新

    #include<cstdio>
    #include<cstring>
    #include <iostream>
    #define MAXN 1005
    #define INF 1<<29
    using namespace std;
        int n,m;
        int num[MAXN][MAXN];
        void dij(){
            int dp[MAXN];
            int vis[MAXN];
            for(int i=1;i<=n;i++){
                dp[i]=num[1][i];
                vis[i]=0;
            }
            vis[1]=1;
            for(int i=1;i<=n;i++){
                int min=INF,sw=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp[j]<min){
                        sw=j;
                        min=dp[j];
                    }
                vis[sw]=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp[j]>dp[sw]+num[sw][j]) dp[j]=dp[sw]+num[sw][j];
            }
            printf("%d
    ",dp[n]);
        }
    int main()
    {
        while(~scanf("%d %d",&m,&n)){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(i==j) num[i][j]=0;
                    else num[j][i]=num[i][j]=INF;
            while(m--){
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                if(c<num[a][b]) num[a][b]=num[b][a]=c;
            }
            dij();
        }
        return 0;
    }

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

      wa了超级多次,结果告诉我不能用g++交,要用c++,哇

      不过还是直接用dij算法跑过去就行了

      

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #define MAXN 305
    #define INF 1e9
    using namespace std;
    double num[MAXN][MAXN];
    int n,m;
        void dijkstra(int x){
            for(int k=1;k<=n;k++)
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                        num[i][j]=min(num[i][j],max(num[i][k],num[k][j]));
            return ;
        }
    int main()
    {
        int x[MAXN],y[MAXN];
        int text=1;
        while(cin>>n&&n!=0){
        //printf("Scenario #%d
    ",text++);
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
                scanf("%d%d",&x[i],&y[i]);
            for(int i=1; i<=n; i++)
                for(int j=i+1; j<=n; j++)
                    num[i][j]=num[j][i]=sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
    
        dijkstra(1);
        printf("Scenario #%d
    Frog Distance = %.3lf
    
    ",text++,num[1][2]);
        }
        return 0;
    }

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

      思路:就是dij算法跑呀,但是不知道为什么一开始写的dij一直tle,后面重新写了一遍就过了

    就把两份代码都发一遍吧

    第一份 TLE

    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdio>
    #define MAXN 1005
    using namespace std;
    int num[MAXN][MAXN];
    int dp[MAXN];
    int vis[MAXN];
    int n;
     void dij(){
        for(int i=1;i<=n;i++){
                vis[i]=0;
                dp[i]=num[1][i];
        }
        vis[1]=1;
        for(int i=1;i<=n;i++){
            int maxx=0;
            int t;
            for(int j=1;j<=n;j++)
                if(!vis[j]&&dp[j]>maxx) maxx=dp[t=j];
            vis[t]=1;
            for(int k=1;k<=n;k++)
                if(!vis[k]) dp[k]=max(dp[k],min(dp[t],num[t][k]));
        }
        return ;
     }
    int main(){
        int t;
        int m;
        cin>>t;
        for(int i=1;i<=t;i++){
            cin>>n>>m;
            memset(num,0,n*sizeof(num[0]));
            for(int i=0;i<m;i++){
                int a,b,c;
                cin>>a>>b>>c;
                num[a][b]=num[b][a]=c;
            }
            dij();
            printf("Scenario #%d:
    %d
    
    ",i,dp[n]);
        }
        return 0;
    }

    第二份 AC

    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdio>
    #define MAXN 1005
    using namespace std;
    int num[MAXN][MAXN];
    int dp[MAXN];
    int vis[MAXN];
    int n;
     void dij(){
        for(int i=1;i<=n;i++){
                vis[i]=0;
                dp[i]=num[1][i];
        }
        vis[1]=1;
        for(int i=1;i<=n;i++){
            int maxx=0;
            int t;
            for(int j=1;j<=n;j++)
                if(!vis[j]&&dp[j]>maxx) maxx=dp[t=j];
            vis[t]=1;
            for(int k=1;k<=n;k++)
                if(!vis[k]) dp[k]=max(dp[k],min(dp[t],num[t][k]));
        }
        return ;
     }
    int main(){
        int t;
        int m;
        cin>>t;
        for(int i=1;i<=t;i++){
            cin>>n>>m;
            memset(num,0,sizeof(num));
            for(int i=0;i<m;i++){
                int a,b,c;
                cin>>a>>b>>c;
                num[a][b]=num[b][a]=c;
            }
            dij();
            printf("Scenario #%d:
    %d
    
    ",i,dp[n]);
        }
        return 0;
    }

    D - Silver Cow Party

    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: NM, 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.
     思路:单向边的图,要从1到n然后又从n到1,就跑两边dij算法呗
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <cstdio>
    #define MAXN 1005
    #define INF 1<<30
    using namespace std;
    int num[MAXN][MAXN];
    int dp[MAXN];
    int vis[MAXN];
    int dp1[MAXN];
    int n;
        void dij(int x){
            for(int i=1;i<=n;i++){
                dp[i]=num[i][x];
                vis[i]=0;
            }
            vis[x]=1;
            for(int i=2;i<=n;i++){
                int min=INF,sw=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp[j]<min){
                        sw=j;
                        min=dp[j];
                    }
                if(min==INF) break;
                vis[sw]=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp[j]>dp[sw]+num[j][sw]) dp[j]=dp[sw]+num[j][sw];
            }
        }
        void dij1(int x){
            for(int i=1;i<=n;i++){
                dp1[i]=num[x][i];
                vis[i]=0;
            }
            vis[x]=1;
            for(int i=2;i<=n;i++){
                int min=INF,sw=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp1[j]<min){
                        sw=j;
                        min=dp1[j];
                    }
                if(min==INF) break;
                vis[sw]=1;
                for(int j=1;j<=n;j++)
                    if(vis[j]==0&&dp1[j]>dp1[sw]+num[sw][j]) dp1[j]=dp1[sw]+num[sw][j];
            }
        }
    int main()
    {
        int m,x;
        while(cin>>n>>m>>x){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j) num[i][j]=0;
                num[i][j]=INF;
            }
        }
        for(int i=0;i<m;i++){
            int a,b,t;
            cin>>a>>b>>t;
            if(t<num[a][b]) num[a][b]=t;
        }
        dij(x);
        /*
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
            swap(num[i][j],num[j][i]);
            }
        }
        */
        dij1(x);
        int maxn=0;
        //for(int i=1;i<=n;i++) cout<<dp[i]<<' '<<dp1[i]<<endl;
        for(int i=1;i<=n;i++){
            if(dp[i]!=INF&&dp1[i]!=INF)
                maxn=max(maxn,dp[i]+dp1[i]);
        }
        cout<<maxn<<endl;
        }
        return 0;
    }

      

      

  • 相关阅读:
    Java面试题
    删除两个相同的数据
    Oracle中的rowid rownum
    SQL的七种连接
    Oracle的分页和MySQL的分页
    Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql) (文档 ID 1064487.1)
    Analytic Functions in Oracle
    Oracle X$Tables
    Microsoft SQL Server Trace Flags
    Oracle Log Block Size
  • 原文地址:https://www.cnblogs.com/maybe96/p/9605278.html
Copyright © 2020-2023  润新知