zoukankan      html  css  js  c++  java
  • 最短路练习

    最近准备练习一下最短路,详情见vjudge //[kuangbin带你飞]专题四 最短路练习

    1.POJ 2387

    A - Til the Cows Come Home
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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

     题意:给你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
    ",dis[m]);
    68     }
    69     return 0;
    70 }
    View Code
    2.POJ 2253 
    B - Frogger
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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

    题意:给你几个点的坐标,从第一个点到第二个点,有很多种路径,求其中的一条路径的最长边是其他路径中的最短的

    思路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
    ",cnt);
    87         printf("Frog Distance = %.3f
    
    ",dis[2]);
    88     }
    89     return 0;
    90 }
    View Code

     思路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
    ",cnt);
    63         printf("Frog Distance = %.3f
    
    ",map[1][2]);
    64     }
    65     return 0;
    66 }
    View Code

    3.POJ 1793 

    C - Heavy Transportation
    Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

    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
    

    题意:有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:
    %d
    ",cnt,dis[n]);
    66         if(cnt!=t)
    67             cout<<endl;
    68         
    69     }
    70 }
    View Code

     4.POJ 3268

    D - Silver Cow Party
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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: NM, and X
    Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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.

     题意:有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
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 using namespace std;
     8 #define N 1010
     9 #define INF 999999999
    10 struct node
    11 {
    12     int to;
    13     int cost;
    14 };
    15 vector<node> G[N];
    16 int dis[N][N];
    17 int ans[N*N];
    18 int n,m,x;
    19 bool vis[N];
    20 void addedge(int from,int to,int cost)
    21 {
    22     node e;
    23     e.to=to;
    24     e.cost=cost;
    25     G[from].push_back(e);
    26 }
    27 void spfa(int y)
    28 {
    29     memset(vis,false,sizeof(vis));
    30     
    31     for(int i=1;i<=n;i++)
    32         dis[y][i]=INF;
    33     dis[y][y]=0;
    34     queue<int> q;
    35     q.push(y);
    36     vis[y]=true;
    37     while(!q.empty())
    38     {
    39         int v;
    40         v=q.front();
    41         q.pop();
    42         for(int i=0;i<G[v].size();i++)
    43         {
    44             node p=G[v][i];
    45             if(dis[y][p.to]>dis[y][v]+p.cost)
    46             {
    47                 dis[y][p.to]=dis[y][v]+p.cost;
    48                 if(!vis[p.to])
    49                 {
    50                     vis[p.to]=true;
    51                     q.push(p.to);
    52                 }
    53             }
    54         }
    55         vis[v]=false;
    56     }
    57 }
    58 int main()
    59 {
    60     while(cin>>n>>m>>x)
    61     {
    62         for(int i=1;i<=m;i++)
    63         {
    64             int a,b,c;
    65             scanf("%d %d %d",&a,&b,&c);
    66             addedge(a,b,c);
    67         }
    68         for(int i=1;i<=n;i++)
    69             spfa(i);
    70         for(int i=1;i<=n;i++)
    71         {
    72             ans[i]=dis[i][x]+dis[x][i];
    73         }
    74         sort(ans+1,ans+1+n);
    75         printf("%d
    ",ans[n]);
    76     }
    77     return 0;
    78 }
    View Code

    第二种思路:

    3268 Accepted 968K 63MS G++ 1558B
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 using namespace std;
     8 #define N 1010
     9 #define INF 999999999
    10 struct node
    11 {
    12     int to;
    13     int cost;
    14 };
    15 vector<node> G1[N];
    16 vector<node> G2[N];
    17 int dis1[N];
    18 int dis2[N];
    19 int ans[N*N];
    20 int n,m,x;
    21 bool vis[N];
    22 void addedge(int from,int to,int cost,int flag)
    23 {
    24     node e;
    25     e.to=to;
    26     e.cost=cost;
    27     if(flag==1)
    28         G1[from].push_back(e);
    29     else
    30         G2[from].push_back(e);
    31 }
    32 void spfa(vector<node>G[N],int dis[],int len)
    33 {
    34     
    35     memset(vis,false,sizeof(vis));
    36     memset(dis,0x3f,len);
    37     dis[x]=0;
    38     queue<int> q;
    39     q.push(x);
    40     vis[x]=true;
    41     while(!q.empty())
    42     {
    43         int v;
    44         v=q.front();
    45         q.pop();
    46         for(int i=0;i<G[v].size();i++)
    47         {
    48             node p=G[v][i];
    49             if(dis[p.to]>dis[v]+p.cost)
    50             {
    51                 dis[p.to]=dis[v]+p.cost;
    52                 if(!vis[p.to])
    53                 {
    54                     vis[p.to]=true;
    55                     q.push(p.to);
    56                 }
    57             }
    58         }
    59         vis[v]=false;
    60     }
    61 }
    62 int main()
    63 {
    64     while(cin>>n>>m>>x)
    65     {
    66         for(int i=1;i<=m;i++)
    67         {
    68             int a,b,c;
    69             scanf("%d %d %d",&a,&b,&c);
    70             addedge(a,b,c,1);
    71             addedge(b,a,c,2);
    72         }
    73         spfa(G1,dis1,sizeof(dis1));
    74         spfa(G2,dis2,sizeof(dis2));
    75         for(int i=1;i<=n;i++)
    76             ans[i]=dis1[i]+dis2[i];
    77         sort(ans+1,ans+1+n);
    78         printf("%d
    ",ans[n]);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    HDU 4714:Tree2cycle 树形DP
    HDU 4679:Terrorist’s destroy 树形DP
    as 和is的区别
    关于父类引用指向子类对象
    C# new的用法
    Mvc中把list从View传入Controller
    Html.TextBoxFor三元判断
    ref 和out的用法以及区别
    c# datatable list 相互转换
    jquery trigger伪造a标签的click事件取代window.open方法
  • 原文地址:https://www.cnblogs.com/as3asddd/p/5454966.html
Copyright © 2011-2022 走看看