P2966 [usaco 09dec] Cow Toll Paths, p2966usaco 09dec

Source: Internet
Author: User

P2966 [usaco 09dec] Cow Toll Paths, p2966usaco 09dec
Description

Like everyone else, FJ is always thinking up ways to increase his revenue. to this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm.

The cows move from any of the N (1 <= N <= 250) pastures conveniently numbered 1 .. N to any other pasture over a set of M (1 <= M <= 10,000) bidirectional cowpaths that connect pairs of different pastures A_j and B _j (1 <= A_j <= N; 1 <= B _j <= N ). FJ has assigned a toll L_j (1 <= L_j <= 100,000) to the path connecting pastures A_j and B _j.

While there may be multiple cowpaths connecting the same pair of pastures, a cowpath will never connect a pasture to itself. best of all, a cow can always move from any one pasture to any other pasture by following some sequence of cowpaths.

In an act that can only be described as greedy, FJ has also assigned a toll C_ I (1 <= C_ I <= 100,000) to every pasture. the cost of moving from one pasture to some different pasture is the sum of the tolls for each of the cowpaths that were traversed plus a * single additional toll * that is the maximum of all the pasture tolls encountered along the way, including the initial and destination pastures.

The patient cows wish to investigate their options. they want you to write a program that accepts K (1 <= K <= 10,000) queries and outputs the minimum cost of trip specified by each query. query I is a pair of numbers s_ I and t_ I (1 <= s_ I <= N; 1 <= t_ I <= N; s_ I! = T_ I) specifying a starting and ending pasture.

Consider this example digoal with five pastures:

The 'edge toll 'for the path from pasture 1 to pasture 2 is 3. Pasture 2's 'node toll' is 5.

To travel from pasture 1 to pasture 4, traverse pastures 1 to 3 to 5 to 4. this incurs an edge toll of 2 + 1 + 1 = 4 and a node toll of 4 (since pasture 5s toll is greatest ), for a total cost of 4 + 4 = 8.

The best way to travel from pasture 2 to pasture 3 is to traverse pastures 2 to 5 to 3. this incurs an edge toll of 3 + 1 = 4 and a node toll of 5, for a total cost of 4 + 5 = 9.

Like everyone else, John, the farmer, is teaching me how to take advantage of the world and how to make money. In order to make a fortune, he has set up a series of rules and regulations, so that any cow on the road in the farm will have to pay the toll to the farmer John. The farm contains N (1 <= N <= 250) lawns (marked as 1 to N) and M (1 <= M <= 10000) A bidirectional road is connected to A_j and B _j (1 <= A_j <= N; 1 <= B _j <= N ).

Cows can go to any lawn from any place of grass. FJ has set a toll (1 <= L_j <= 100,000) on the two-way road connecting A_j and B _j ). There may be multiple roads connecting the same two lawns, but there is no road connecting a lawn and the grass itself. Fortunately, cows can always go to any other lawn through a series of paths. In addition to being greedy, animals do not know what to say.

FJ sets a toll C_ I (1 <= C_ I <= 100000) on each lawn ). The cost of a lawn to another lawn is the sum of the tolls on all the roads that pass through, plus the maximum amount of tolls on all the lawns that pass through (including the start and end points. The hard-working ox hopes to find out which path they should choose.

They want you to write a program, accept K (1 <= K <= 10,000) questions, and output the minimum cost for each query. Question I contains two numbers: s_ I and t_ I (1 <= s_ I <= N; 1 <= t_ I <= N; s_ I! = T_ I), indicating the lawn of the start and end points.

Input/Output Format Input Format:
  • Line 1: Three space separated integers: N, M, and K

  • Lines 2. N + 1: Line I + 1 contains a single integer: C_ I

  • Lines N + 2. N + M + 1: Line j + N + 1 contains three space separated

Integers: A_j, B _j, and L_j

  • Lines N + M + 2 .. N + M + K + 1: Line I + N + M + 1 specifies query I using two space-separated integers: s_ I and t_ I
Output Format:
  • Lines 1. K: Line I contains a single integer which is the lowest cost of any route from s_ I to t_ I
Input and Output sample Input example #1:
5 7 2 2 5 3 3 4 1 2 3 1 3 2 2 5 3 5 3 1 5 4 1 2 4 3 3 4 4 1 4 2 3 
Output sample #1:
8 9 

We will introduce a method for AC without sorting.

(Please refer to this great god's blog: http://www.cnblogs.com/peter-le/p/6014643.html sort method)

We can respectively record the length of the shortest path from point x to point y and the cost of the shortest path.

Then there is a set of Floyd templates.

But note that because your length and cost are recorded separately

Therefore, Floyd does not necessarily have the minimum value.

We can try it several more times.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=251; 7 const int maxn=0x7ffffff; 8 void read(int & n) 9 {10     char c='+';int x=0;    11     while(c<'0'||c>'9')c=getchar();12     while(c>='0'&&c<='9')13     {14         x=x*10+c-48;15         c=getchar();16     }17     n=x;18     19 }20 int a[MAXN][MAXN];21 int spend[MAXN][MAXN];22 int b[MAXN];23 int n,m,q;24 void floyd()25 {26     for(int k=1;k<=n;k++)27         for(int i=1;i<=n;i++)28             for(int j=1;j<=n;j++)29             {30                 int p=a[i][k]+a[k][j]+max(spend[i][k],spend[k][j]);31                 if((p<a[i][j]+spend[i][j])&&a[i][k]<maxn&&a[k][j]<maxn)32                 {33                     //a[i][j]=a[i][j]-max(spend[i],spend[j]);34                     a[i][j]=a[i][k]+a[k][j];35                     spend[i][j]=max(spend[i][k],spend[k][j]);36                 }            37             }38 }39 int main()40 {41     read(n);read(m);read(q);42     for(int i=1;i<=n;i++)43         read(b[i]);44     for(int i=1;i<=n;i++)45         for(int j=1;j<=n;j++)46         {47             if(i==j)48             a[i][j]=0;49             else50             a[i][j]=maxn;51         }52             53     for(int i=1;i<=m;i++)54     {55         int x,y,z;56         read(x);read(y);read(z);57         if(a[x][y]>z)58         {59             a[x][y]=z;60             a[y][x]=z;61             spend[x][y]=max(b[x],b[y]);62             spend[y][x]=max(b[x],b[y]);63         }64     65     }66     floyd();67     floyd();68     floyd();            69     for(int i=1;i<=q;i++)70     {71         int x,y;72         read(x);read(y);73         printf("%d\n",a[x][y]+spend[x][y]);74     }75     return 0;76 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.