Meeting(未建边的最短路)
来源:互联网 发布:javascript 资源 网盘 编辑:程序博客网 时间:2024/05/01 21:36
第一次接触这种题目,a了一天,终于a过了,赶紧上课去
http://acm.hdu.edu.cn/showproblem.php?pid=5521
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 317 Accepted Submission(s): 92
Problem Description
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
fences they were separated into different blocks. John's farm are divided inton blocks labelled from 1 to n .
Bessie lives in the first block while Elsie lives in then -th one. They have a map of the farm
which shows that it takes theyti minutes to travel from a block in Ei to another block
inEi where Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
fences they were separated into different blocks. John's farm are divided into
Bessie lives in the first block while Elsie lives in the
which shows that it takes they
in
and which block should be chosen to have the meeting.
Input
The first line contains an integer T (1≤T≤6) , the number of test cases. Then T test cases
follow.
The first line of input containsn and m . 2≤n≤105 . The following m lines describe the sets Ei (1≤i≤m) . Each line will contain two integers ti(1≤ti≤109) and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei . It is guaranteed that ∑mi=1Si≤106 .
follow.
The first line of input contains
Output
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
Sample Input
25 41 3 1 2 32 2 3 410 2 1 53 3 3 4 53 11 2 1 2
Sample Output
Case #1: 33 4Case #2: Evil JohnHintIn the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.
熟悉的迪杰斯特拉代码
#include <cstdio>#include <iostream>#include <queue>#include <vector>#include <cstring>using namespace std;const int N=100010;const long long maxn=1LL<<60;int n,m,s,t[N],a;vector<vector<int> > G,se;long long dis1[N],dis2[N];bool used1[N],used2[N];struct node{ int v; long long w; node(int vv,long long ww):v(vv),w(ww){} bool operator <(const node &a)const { return w>a.w; }};void dijst(long long *dis,int s){ priority_queue<node> q; memset(used1,0,sizeof(used1)); memset(used2,0,sizeof(used2)); for(int i=1;i<=n;i++) dis[i]=maxn; q.push(node(s,0)); while(!q.empty()){ node p=q.top(); q.pop(); int u=p.v; if(used1[u])continue; used1[u]=true; dis[u]=p.w; for(int i=0;i<G[u].size();i++){ int x=G[u][i]; if(used2[x])continue; used2[x]=true; int w=t[x]; for(int j=0;j<se[x].size();j++){ int v=se[x][j]; if(used1[v]||dis[v]<dis[u]+w)continue; dis[v]=dis[u]+w; q.push(node(v,dis[v])); } } }}int main(){ int T; scanf("%d",&T); for(int cases=1;cases<=T;cases++){ scanf("%d%d",&n,&m); G.clear(); se.clear(); G.resize(n+10); se.resize(m+10); for(int i=1;i<=m;i++){ scanf("%d%d",&t[i],&s); for(int j=1;j<=s;j++){ scanf("%d",&a); G[a].push_back(i); se[i].push_back(a); } } dijst(dis1,1); dijst(dis2,n); long long ans=maxn; for(int i=1;i<=n;i++){ dis1[i]=max(dis1[i],dis2[i]); ans=min(dis1[i],ans); } if(ans==maxn){ printf("Case #%d: Evil John\n",cases); continue; } int cnt=0; for(int i=1;i<=n;i++) if(dis1[i]==ans) dis2[cnt++]=i; printf("Case #%d: %I64d\n",cases,ans); printf("%I64d",dis2[0]); for(int i=1;i<cnt;i++) printf(" %I64d",dis2[i]); printf("\n"); } return 0;}
0 0
- Meeting(未建边的最短路)
- HDU5521 Meeting (最短路)
- HDU 5521 Meeting(巧妙地构图的最短路)
- HDU 5521 Meeting 化简边的最短路
- HDU 5521 Meeting (最短路)
- hdu 5521 Meeting(最短路)
- Hdu 5521 Meeting(建图+最短路)
- HDU 5521:Meeting(最短路)
- hdu5521 Meeting (最短路)
- hdu5521 Meeting最短路
- HDU5521 Meeting【最短路】
- HDU 5521-Meeting(最短路)
- HDU 5521 Meeting 最短路
- HDU 5521 Meeting【最短路】
- HDU 5521 Meeting(最短路)
- HDU 5521 Meeting(最短路)
- 【最短路】hdu 5521 Meeting
- 最短路(建图)—— Meeting (HDU 5521)
- 328 (Div. 2) B
- Qt小总结----QSplitter、QDockWidget、QToolBar、布局
- java web利用 ajax 返回 json
- Hibernate面试题及答案
- 在c中使用mp4v2的库——准备工作
- Meeting(未建边的最短路)
- C++用new和不用new创建类对象区别
- 缓冲区
- C++实现二叉搜索树和AVL树
- Struts面试题
- 前端构建工具gulpjs的使用介绍及技巧
- 黑马程序员---多线程小结
- 初始化GDI+库进行图形图像处理
- 代码测试、调试与优化小结