HDU5521Meeting(最短路建图)
来源:互联网 发布:邓肯季后赛数据 编辑:程序博客网 时间:2024/05/21 08:54
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2988 Accepted Submission(s): 952
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.
Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意:
给n个点,m个集合,每个集合里有si 个点,相同集合内部点的距离为fi,A在1号点,B在n号点,A B要见面,他们只能在节点见面不可以在路上见面,AB走单位距离话费1分钟,问最少话费几分钟可以见面,在最小话费的前提下有几个节点可以作为见面的点,按照字典序输出可以见面的地点,如果没有输出Evil John 。
思路:
这道题目的难点在于没有办法给所有点两两之间建边,因为点的个数是100000,但是这个题给了集合这个东西,在每个集合里两点之间的距离是固定的,所以我们可以增加m个点,每个集合增加一个点,然后把所有点的出边指向新增的点,权值为w,然后再从新增的点指回来,权值为0,这样的话相当于建边的时间复杂度从n方降为2n,这样的话一切就都容易了。
#include<bits/stdc++.h>using namespace std;typedef long long ll;#define pii pair<ll, int>const int maxn = 2e5+5;const int maxe = 2e6+5;const ll INF = 0x3f3f3f3f3f3f3f3f;int head[maxn], cnt;int ans[maxn];ll dis[maxn][2];bool vis[maxn];struct Edge{ int v, nxt; ll w; Edge(){} Edge(int v, ll w, int nxt):v(v), w(w), nxt(nxt){}}edge[maxe];void addedge(int from, int to, ll w){ edge[cnt] = Edge(to, w, head[from]); head[from] = cnt++;}void Dijkstra(int s, int k){ memset(vis, false, sizeof(vis)); dis[s][k] = 0; priority_queue<pii, vector<pii>, greater<pii> > q; q.push(pii(dis[s][k], s)); while(!q.empty()) { pii temp = q.top(); q.pop(); int u = temp.second; if(vis[u]) continue; vis[u] = true; for(int i = head[u]; i != -1; i = edge[i].nxt) { int to = edge[i].v; if(dis[to][k] > dis[u][k] + edge[i].w) { dis[to][k] = dis[u][k] + edge[i].w; q.push(pii(dis[to][k], to)); } } }}int main(){ int t, n, m, s, u, v; scanf("%d", &t); for(int kase = 1; kase <= t; kase++) { cnt = 0; memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for(int k = 1; k <= m; k++) { ll w; scanf("%I64d%d", &w, &s); for(int i = 1; i <= s; i++) { scanf("%d", &u); addedge(u, n+k, w); addedge(n+k, u, 0); } } for(int i = 1; i <= n+m; i++) dis[i][0] = dis[i][1] = INF; Dijkstra(1, 0); Dijkstra(n, 1); int tol = 0; ll ma = INF; for(int i = 1; i <= n; i++) { ll ret = max(dis[i][0], dis[i][1]); if(ret < ma) { ma = ret; tol = 0; ans[tol++] = i; } else if(ret == ma && ret != INF) ans[tol++] = i; } if(tol == 0) printf("Case #%d: Evil John\n", kase); else { printf("Case #%d: %d\n", kase, ma); for(int i = 0; i < tol; i++) printf("%d%c", ans[i], i == tol-1 ? '\n' : ' '); } } return 0;}
阅读全文
0 0
- HDU5521Meeting(最短路建图)
- hdu5521Meeting【最短路】2015沈阳现场赛
- hdu5521Meeting(dijkstra最短路)icpc沈阳赛区M题
- hdu5521Meeting/2015acmicpc沈阳3号题(图论最短路)
- 最短路(最短路)
- HDOJ2544 最短路(最短路)
- 最短路 (Dijstra) 最短路
- HDU2544:最短路(最短路)
- ICPC2015(沈阳)HDU5521 建图技巧+最短路
- Hdu 5512 Meeting (建图技巧 + 最短路)
- Proxy 最短路 反向建图
- GalaxyOJ-988 (套路建图+最短路)
- Aizu1311 分层图最短路 (...大概)
- BZOJ1001(对偶图+最短路)
- 【图-最短路】 gentree
- 图的最短路
- hdu2544(最短路)
- 随笔(最短路)
- AnimatorSet
- ACM DP Max Sum Plus Plus
- 用C++实现单链表的基本函数以及增删查改
- 解决带有日期格式的txt数据导入oracle报错的问题
- 用PHP做一个小的日历程序
- HDU5521Meeting(最短路建图)
- VPS 面板
- Leetcode 152(Java)
- 二维图像中EMD 法与 Mura 检测的总结
- java并发编程实战-Java内存模型
- NYOJ 题目71 独木舟上的旅行
- jQuery Validate 小案例,让你了解jQueryValidate
- 算法研究、编程艺术、红黑树、数据挖掘、程序员面试5大系列集锦
- 使用Strophe.js基于xmpp服务实现即时通讯