hdu 5521 Meeting(优先队列+dijkstra)(建图思想)
来源:互联网 发布:痘痘变硬了怎么办知乎 编辑:程序博客网 时间:2024/06/07 10:36
Meeting
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 into n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei 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.
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 contains n 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.
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.
Sample Input
2
5 4
1 3 1 2 3
2 2 3 4
10 2 1 5
3 3 3 4 5
3 1
1 2 1 2
Sample Output
Case #1: 3
3 4
Case #2: Evil John
Hint
In 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.
题意:给你n个点和m个集合,集合内的点可以相互到达,所花费的时间都为
思路:对每个集合建立源点,源点到集合中的点的权值和集合中的点到源点的权值都为w/2,这样才能使得集合中的点之间的权值为w
然后从1开始跑一遍最短路,从n开始跑一遍最短路,遍历找到最小值即可
代码:
#include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;const int maxn=1e6+10;struct node{ int u,dis; node() {} node(int U,int DIS) { u=U,dis=DIS; } bool operator < (const node&A)const { return dis>A.dis; }};struct edge{ int v,w,next;} E[maxn<<1];int dis1[maxn],dis2[maxn],first[maxn];bool inq[maxn];int n,m,len;void Add_edge(int u,int v,int w){ E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++;}void Dijkstra(int st,int dis[]){ for(int i=1;i<maxn;++i) dis[i]=inf; memset(inq,false,sizeof(inq)); priority_queue<node>q; q.push(node(st,0)); dis[st]=0; while(!q.empty()) { node p=q.top(); q.pop(); if(inq[p.u]) continue; inq[p.u]=true; for(int i=first[p.u]; ~i; i=E[i].next) { int v=E[i].v,w=E[i].w; if(dis[v]>dis[p.u]+w) { dis[v]=dis[p.u]+w; q.push(node(v,dis[v])); } } }}int main(){ int t,Case=0; scanf("%d",&t); while(++Case<=t) { memset(first,-1,sizeof(first)); len=0; scanf("%d%d",&n,&m); int w,k,x; for(int i=1; i<=m; ++i) { scanf("%d%d",&w,&k); for(int j=1; j<=k; ++j) { scanf("%d",&x); Add_edge(x,n+i,w);//权值为w,防止浮点型计算 Add_edge(n+i,x,w);//权值为w } } Dijkstra(1,dis1); Dijkstra(n,dis2); int Minn=inf; for(int i=1; i<=n; ++i) Minn=min(max(dis1[i],dis2[i]),Minn); printf("Case #%d: ",Case); if(Minn==inf) printf("Evil John\n"); else { printf("%.0lf\n",Minn*1.0/2.0);//因为权值为w,所以最后要除以2 bool flag=false; for(int i=1; i<=n; ++i) if(max(dis1[i],dis2[i])==Minn) if(flag) printf(" %d",i); else printf("%d",i),flag=true; puts(""); } } return 0;}
- hdu 5521 Meeting(优先队列+dijkstra)(建图思想)
- hdoj 5521 Meeting 【优先队列 dijkstra】
- dijkstra(优先队列) HDU 1874
- 2015ACM/ICPC亚洲区沈阳站 Meeting HDU 5521 (优先队列搜索)
- HDU5521 Meeting(dijkstra+优先队列优化)
- HDU-5521 Meeting(Dijkstra)
- hdoj1874 (优先队列+Dijkstra)
- Dijkstra模板(优先队列)
- [hdu 5521 Meeting] 拆边 + Dijkstra
- hdu 2112 dijkstra+优先队列
- hdu 1596 dijkstra+优先队列
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- 最短路(Dijkstra+优先队列)
- Dijkstra算法 用优先队列(思路)
- hdu2544 最短路(dijkstra/优先队列)
- (poj3159)dijkstra优先队列实现
- 堆(优先队列)优化dijkstra(邻接矩阵)
- dijkstra算法模板(优先队列优化)
- Codeforces Round #436 E.Fire(01背包 + 输出路径)
- ros的全局把握
- 2017年10月11日一点点人生感悟~人一定要靠自己
- 比较总结线性表的几种主要存储结果
- hihocoder 1014 Trie树
- hdu 5521 Meeting(优先队列+dijkstra)(建图思想)
- 手动部署 使用 bluestore 后端的 osd (L版)
- 【LeetCode】C# 39、Combination Sum
- CodeForces 868B Race Against Time
- Bootstrap 控制移动端和网页端的元素隐藏和显示
- node vue 环境搭建
- 使用js跨域获取数据的方法
- nkoj 1313(noip2007)树网的核 与 nkoj 2650(SDOI2011)消防
- 在屏幕上输出星号组成的菱形图案。