ZOJ 3396 Conference Call 最短路
来源:互联网 发布:柏林战役 知乎 编辑:程序博客网 时间:2024/06/05 15:00
Good News! Utopia Polycom begins to offer a whole new service, Conference Call. This new service enables three users to make phone calls at the same time and talk to each other. It's very useful when several people are discussing about one shared topic. For example, Tom, Rosemary and you plan to go picnic tomorrow. You just need to make one conference call, so that you can discuss with Tom and Rosemary simultaneously. It's extremely convenient, yes?
However, it has a lot of technological problems to start this service. It took engineers of Utopia Polycom five years to conquer all these problems. One of the biggest problem is how to make the communication line connected. To solve this problem, they have constructed a powerful net of transfer stations. Every single phone is connected to its nearest transfer station, some transfer stations are connected to each other by wires. When to make a conference call, signal will start from the dialling telphone. The signal will be transferred through transfer stations and finally reach the other two phones.
Since the distances between each two transfer stations are various, the cost for the signal to go through the wires is different as well. Assume there are n telphones(numbered from1 to n) and m(numbered from 1 to m) transfer stations in this communication net. The total cost for a line is the sum of the costs of the wires in the line. Our problems remains, what is the minimal total cost to make a conference call among three telphone a, b and c. As showing below, the minimal total cost to make a conference call among telphone 1, 2, 3 is 12.
Input
There are multiple cases. For each test case, the first line contain three positive integers, n (n ≤ 10000), m (m ≤ 500), l (l ≤ 10000). l means the number of wires. Then follows n lines. Each line contains one integer ti (1 ≤ ti ≤ m), which means the number of transfer station No.i telphone is connected to. Then follows l lines, each line contains three integers a, b, C(1 ≤ C ≤ 500), which means a and b transfer stations are connected to each other by a wire with the amount of cost C. If a pair (a, b) doesn't appear in these l lines, that means tranfer stations a, b are not connected.
The next line is a single integer q (q ≤ 50). Then follow q lines. Each line contains three integers x, y, z. You are supposed to calculate the minimal total cost to make a conference call among telphone x, y, z. Each test case is seperated by a blank line. Proceed to the end of the file.
Output
For each test case i, print case number in the form "Case #i" in one single line. Then for each inquiry j, print one line in the form "Line j: " and if the conference call can be made and the minimal total cost is Min, print the sentense "The minimum cost for this line is Min." else you should print "Impossible to connect!". Look at the samples for details.
Sample Input
3 5 52151 2 62 3 13 4 24 5 31 5 1211 2 33 3 11232 3 411 2 3
Sample Output
Case #1Line 1: The minimum cost for this line is 12.Case #2Line 1: Impossible to connect!
Author: ZHU, Yuke
Contest: ZOJ Monthly, September 2010
对三个点跑一遍最短路,然后枚举m个点作为中间点,取最小值
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis) memset(vis,0,sizeof(vis))#define MST(vis,pos) memset(vis,pos,sizeof(vis))#define MAX3(a,b,c) max(a,max(b,c))#define MAX4(a,b,c,d) max(max(a,b),max(c,d))#define MIN3(a,b,c) min(a,min(b,c))#define MIN4(a,b,c,d) min(min(a,b),min(c,d))#define PI acos(-1.0)#define INF 0x7FFFFFFF#define LINF 1000000000000000000LL#define eps 1e-8const int maxn=555;int pos[maxn*20];int mp[maxn][maxn],dis[maxn][maxn];int flag[maxn];int vis[maxn];int n,m,l;void init(){ CLR(flag); REP(i,maxn) { REP(j,maxn) mp[i][j]=-1,dis[i][j]=-1; mp[i][i]=0,dis[i][i]=0; }}void dijkstra(int from,int n){ int s=from; int g,res; CLR(vis); vis[s]=1; REP(i,n) { g=INF; FOR(j,1,n) { if(!vis[j]) { if(mp[s][j]!=-1 && dis[from][j]>dis[from][s]+mp[s][j] ) dis[from][j]=dis[from][s]+mp[s][j]; else if(mp[s][j]!=-1 && dis[from][j]==-1 ) dis[from][j]=dis[from][s]+mp[s][j]; if(g>dis[from][j]&&dis[from][j]!=-1 ) { g=dis[from][j]; res=j; } } } if(g==INF) break; s=res; vis[s]=1; }}bool check(int a,int b,int c ){ if(dis[a][b]==-1 || dis[a][c]==-1) return false; return true;}int main(){ int cas=1; while(scanf("%d%d%d",&n,&m,&l)!=EOF) { init(); FOR(i,1,n) scanf("%d",&pos[i]); int u,v,w; REP(i,l) { scanf("%d%d%d",&u,&v,&w); mp[u][v]=mp[v][u]=w; } int q; scanf("%d",&q); printf("Case #%d\n",cas++); int cnt=1; int a,b,c; while(q--) { scanf("%d%d%d",&a,&b,&c); a=pos[a],b=pos[b],c=pos[c]; if(!flag[a]) flag[a]=1,dijkstra(a,m); if(!flag[b]) flag[b]=1,dijkstra(b,m); if(!flag[c]) flag[c]=1,dijkstra(c,m); if(check(a,b,c)) { v=INF; FOR(i,1,m) if(v>dis[a][i]+dis[b][i]+dis[c][i] ) v=dis[a][i]+dis[b][i]+dis[c][i]; printf("Line %d: The minimum cost for this line is %d.\n",cnt++,v); } else printf( "Line %d: Impossible to connect!\n",cnt++); } }}
- ZOJ 3396 Conference Call 最短路
- ZOJ 3396 Conference Call
- zoj 3396 Conference Call
- zoj(Conference Call)
- zju 3396 Conference Call
- ZOJ 3396 Conference Call 求经过特定3点的最小生成树
- ZOJ 2027 最短路
- ZOJ 2797 最短路
- ZOJ 1082 最短路
- ZOJ 1221 最短路
- ZOJ 1857 最短路
- ZOJ 3794 最短路
- zoj 1221 Risk 最短路
- ZOJ 3378 求割边+最短路
- ZOJ 2504 Help John! 【最短路】 【Dijkstra】
- ZOJ 1655 Transport Goods 【最短路】【Dijkstra】
- zoj 1082 Stockbroker Grapevine 最短路 Dijkstra
- zoj 1952 Heavy Cargo 最短路 Dijkstra
- MySQL随笔
- 测试之路2——对比XML文件1
- 数据持久化
- Android屏幕备选资源
- orcale数据恢复
- ZOJ 3396 Conference Call 最短路
- 面试题之交通灯管理系统
- MM01 BDC录屏实例
- Android 进阶学习:Android自定义View的实现方法,带你一步步深入了解View(四)
- 嵌入tomcat7
- 使用Math.floor和Math.random取随机整数
- Java基础之值传递与引用传递
- Linux进程间通信方式简介
- 5.26-5.27 最近有点闲(烦)