uva_10246_Asterix and Obelix(最短路)
来源:互联网 发布:好听的淘宝优惠群取名 编辑:程序博客网 时间:2024/05/16 06:09
10246 Asterix and Obelix
After winning a gruesome battle against the Romans in a faraway land, Asterix and his dearest friend Obelix are now returning home. However Obelix is not with Asterix now. He has left Asterix in order to delivermenhir to one of his internationalbuyers (as you probably know, recently he has extended his trade to international markets). But he has promised to join Asterix on his way home and Asterix has promised to host a feast for Obelix (you know how fat he is!) in the city they meet. Obelix may meet Asterix in any city on his way home including the starting and the destination city.
Now Asterix is sitting with a map and trying to figure out the cheapest route home. The map shows the cities and the cost (in sestertii) of going from one city to another if there is a road connecting them directly. For each city in the map Asterix has also calculated the cost (in sestertii) of hosting a feast for Obelix in that city. There will be only one feast and for safety Asterix has decided to set aside enough sestertii to host a feast in the costliest city on the route.
Since Asterix does not have a computer, he seeks your help to find out the cheapest route home.
Input
The input may contain multiple test cases.
The first line of each test case contains three integers C(≤ 80), R (≤ 1000) and Q (≤ 6320) where C indicates the number of cities (cities are numbered using distinct integers ranging from 1 to C), R represents the number of roads and Q is the number of queries.
The next line contains C integers where the i-th integer fi is the cost (in sestertii) of hosting a feast in city i.
Each of the next R lines contains three integers: c1, c2 (̸= c1)and d indicating that the cost of going from city c1 to c2 (or from c2 to c1) is d sestertii.
Each of the next Q lines contains two integers c1 and c2 (c1 ̸= c2) asking for the cost (in sestertii) of the cheapest route from city c1 to city c2.
The input will terminate with three zeros form C, S and Q.
Output
For each test case in the input first output the test case number (starting from 1) as shown in the sample output. Then for each query in the input print a line giving the minimum cost (in sestertii) of going from the first to the second city in the query. If there exists no path between them just print ‘-1’. Print a blank line between two consecutive test cases.
Sample Input
7 8 5
2 3 5 15 4 4 6
1 2 20
1 4 20
1 5 50
2 3 10
3 4 10
3 5 10
4 5 15
6 7 10
1 5
1 6
5 1
3 1
6 7
4 4 2
2 1 8 3
1 2 7
1 3 5
2 4 8
3 4 6
1 4
2 3
0 0 0
Sample Output
Case #1
45
-1
45
35
16
Case #2
18
20
题意:最短路变形。
分析:先用spfa处理出每个点到其他点的最短路(注意:每个点的value都得小于起点的value),用dp数组保存,然后枚举每个点,ans=min( ans,dp[i][u]+dp[i][v]+f[i] )。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28972
代码清单:
#include<map>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int maxv = 80 + 5;const int maxe = 1000 + 5;const int max_dis = 1e9;struct Edge{ int to; int dis; Edge(int to,int dis){ this -> to = to; this ->dis = dis; }};int C,R,Q;int f[maxv];int a,b,c,p,q,ans;int dp[maxv][maxv];int vis[maxv],dist[maxv],Case=0;vector<Edge>G[maxv];void spfa(int s){ fill(vis+1,vis+1+C,0); fill(dist+1,dist+1+C,max_dis); queue<int>q; while(q.size()) q.pop(); dist[s]=0; vis[s]=1; q.push(s); while(q.size()){ int v=q.front(); q.pop(); vis[v]=0; for(int i=0;i<G[v].size();i++){ Edge& e=G[v][i]; if(f[e.to]<=f[s]&&dist[e.to]>dist[v]+e.dis){ //最短路上每个点的value小于起点的value dist[e.to]=dist[v]+e.dis; if(!vis[e.to]){ vis[e.to]=1; q.push(e.to); } } } } for(int i=1;i<=C;i++) dp[s][i]=dist[i];}int main(){ while(scanf("%d%d%d",&C,&R,&Q)!=EOF){ if(C==0&&R==0&&Q==0) break; for(int i=0;i<maxv;i++) G[i].clear(); for(int i=1;i<=C;i++) scanf("%d",&f[i]); for(int i=0;i<R;i++){ scanf("%d%d%d",&a,&b,&c); G[a].push_back(Edge(b,c)); G[b].push_back(Edge(a,c)); } if(Case) printf("\n"); printf("Case #%d\n",++Case); for(int i=1;i<=C;i++) spfa(i); while(Q--){ ans=max_dis; scanf("%d%d",&p,&q); for(int i=1;i<=C;i++){ ans=min(ans,dp[i][p]+dp[i][q]+f[i]); } if(ans==max_dis) ans=-1; printf("%d\n",ans); } }return 0;}
- uva_10246_Asterix and Obelix(最短路)
- UVA 10246 - Asterix and Obelix(最短路)
- uva 10246 - Asterix and Obelix(最短路)
- uva 10246 Asterix and Obelix(最短路问题拓展 dijkstra)
- (beginer) 最短路 UVA 10246 Asterix and Obelix
- UVa 10246 Asterix and Obelix(变形的最短路径)
- UVa10246 - Asterix and Obelix(最短路径变形)
- 10246 - Asterix and Obelix
- uva10246- Asterix and Obelix
- 最短路(最短路)
- UVA1416 Warfare And Logistics(LA4080)(最短路,5级)
- UVALive 4080 Warfare And Logistics(最短路树)
- 图论 之 最短路(Dijkstra and Floyd)
- UVA 1416 - Warfare And Logistics(最短路树)
- E. Paths and Trees (CF 303 div2)最短路
- CodeForces 545E Paths and Trees(最短路)
- UVALive 4080 Warfare And Logistics(Dijkstra+最短路树)
- CodeForces 567E President and Roads(最短路 + tarjan)
- 基站选址
- google 登录 login api php
- RT5350 HLK-RM04模块配置OpenWRT
- Android创建Service后台常驻服务并使用Broadcast通信
- Code Hunt SECTOR 02 LOOPS
- uva_10246_Asterix and Obelix(最短路)
- 分析源代码编译链接过程和shell中ELF格式可执行文件的初始化执行过程
- request.getparameter中文乱码问题解决方案
- 条款27:尽量少做转型操作
- [BZOJ2434]NOI2011阿狸的打字机|AC自动机|fail树|树状数组
- nyoj-220 推桌子
- 设计模式--访问者模式
- android查看kernel log
- 第10题