P2966 [USACO09DEC]牛收费路径Cow Toll Paths
2018-06-17 22:24:36来源:未知 阅读 ()
题目描述
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 diagram 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 5's 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.
跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道。为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费。 农场中由N(1 <= N <= 250)片草地(标号为1到N),并且有M(1 <= M <= 10000)条 双向道路连接草地A_j和B_j(1 <= A_j <= N; 1 <= B_j <= N)。
奶牛们从任意一片草 地出发可以抵达任意一片的草地。FJ已经在连接A_j和B_j的双向道路上设置一个过路费L_j (1 <= L_j <= 100,000)。 可能有多条道路连接相同的两片草地,但是不存在一条道路连接一片草地和这片草地本身。最 值得庆幸的是,奶牛从任意一篇草地出发,经过一系列的路径,总是可以抵达其它的任意一片 草地。 除了贪得无厌,叫兽都不知道该说什么好。
FJ竟然在每片草地上面也设置了一个过路费C_i (1 <= C_i <= 100000)。从一片草地到另外一片草地的费用,是经过的所有道路的过路 费之和,加上经过的所有的草地(包括起点和终点)的过路费的最大值。 任劳任怨的牛们希望去调查一下她们应该选择那一条路径。
她们要你写一个程序,接受K(1 <= K <= 10,000)个问题并且输出每个询问对应的最小花费。第i个问题包含两个数字s_i 和t_i(1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i),表示起点和终点的草地。
输入输出格式
输入格式:-
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
- Lines 1..K: Line i contains a single integer which is the lowest cost of any route from s_i to t_i
输入输出样例
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
8 9
给大家介绍一种不用排序就能AC的方法
(排序的方法请看这位大神的博客:http://www.cnblogs.com/peter-le/p/6014643.html)
我们可以分别记录下从点x到点y 的最短路径的长度和在最短路上的花费
然后就是套Floyd的模板。
但是注意,因为你的长度和花费是分开记录的
所以一遍Floyd跑出来的不一定是最小值
我们可以多跑几遍试试
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 else 50 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 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++进阶训练——停车收费系统设计 2018-09-01
- VS2012程序打包部署详解 2018-06-18
- DotNet 资源大全 2018-06-18
- 3022 西天收费站 2018-06-17
- Luogu P2966 [USACO09DEC]牛收费路径Cow Toll Paths 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash