UVa 10246 Asterix and Obelix(变形的最短路径)
来源:互联网 发布:港台 知乎 编辑:程序博客网 时间:2024/06/05 10:58
10246 - Asterix and Obelix
Time limit: 3.000 seconds
Problem A
Asterix and Obelix
Input: standard input
Output: standard output
Time Limit: 5 seconds
Memory Limit: 32 MB
After winning a gruesome battle against the Romans in a far-away 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 deliver menhir to one of his international buyers (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
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <vector>using namespace std;const int inf=999999999;const int maxn=110;struct node{ int v,t; node(){} node(int _v,int _t):v(_v),t(_t){}};vector <node> G[maxn];int C,R,Q,cnt,dis[maxn][maxn],W[maxn];bool visited[maxn];void spfa(int x){ memset(visited,0,sizeof(visited)); queue <int> q; q.push(x); dis[x][x]=0; visited[x]=1; while(!q.empty()) { int c=q.front(); q.pop(); visited[c]=0; for(int i=0; i<G[c].size(); i++) { int v=G[c][i].v; if(dis[x][v]>dis[x][c]+G[c][i].t && W[x]>=W[v]) { dis[x][v]=dis[x][c]+G[c][i].t; if(!visited[v]) { q.push(v); visited[v]=1; } } } }}void initial(){ for(int i=0;i<maxn;i++) G[i].clear(); for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) dis[i][j]=inf;}void input(){ int x,y,z; for(int i=1;i<=C;i++) scanf("%d",&W[i]); for(int i=0;i<R;i++) { scanf("%d %d %d",&x,&y,&z); G[x].push_back(node(y,z)); G[y].push_back(node(x,z)); }}void solve(){ int x,y; for(int i=1;i<=C;i++) spfa(i); printf("Case #%d\n",++cnt); for(int co=0;co<Q;co++) { scanf("%d %d",&x,&y); int ans=inf; for(int i=1;i<=C;i++) ans=min(ans,dis[i][x]+dis[i][y]+W[i]); if(ans<inf) printf("%d\n",ans); else printf("-1\n"); }}int main(){ while(scanf("%d %d %d",&C,&R,&Q)!=EOF) { if(C==0 && R==0 && Q==0) break; if(cnt) printf("\n"); initial(); input(); solve(); } return 0;}
- UVa 10246 Asterix and Obelix(变形的最短路径)
- UVa10246 - Asterix and Obelix(最短路径变形)
- UVA 10246 - Asterix and Obelix(最短路)
- uva 10246 - Asterix and Obelix(最短路)
- (beginer) 最短路 UVA 10246 Asterix and Obelix
- uva 10246 Asterix and Obelix(最短路问题拓展 dijkstra)
- 10246 - Asterix and Obelix
- uva10246- Asterix and Obelix
- 变形的最短路径 POJ 3621
- Arbitrage 最短路径的变形 flord
- uva_10246_Asterix and Obelix(最短路)
- uva 1416 Warfare And Logistics (最短路径树)
- POJ2253 最短路径变形
- 杭电 hdu 3790 最短路径问题 (最短路径 + Dijkstra)稍微的变形
- HDU 5441 Travel(最短路径变形)
- PAT 紧急救援 (最短路径变形)
- 51nod 1445 变色DNA (最短路径变形)
- POJ 2253 最短路径变形
- JS 取整2位小数
- 微信公众号url配置失败
- vfork 挂掉的一个问题
- 课程设计:题目2
- 华为机试—01背包(等于总值)
- UVa 10246 Asterix and Obelix(变形的最短路径)
- OC学习笔记-自动释放池的创建方式
- 关於 Parcelable 实作以及继承
- 内存数据组织Apache Ignite
- 饿汉式单例和懒汉式单例比较
- ecshop安装
- Head First PHP&MySQL学习笔记(一)
- 【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0、OpenCV 2.4.8、OpenCV 2.4.9 +VS 开发环境配置
- c 结构体,既内存分配