hdoj 5521 Meeting 【优先队列 dijkstra】
2015-11-02 21:46
330 查看
MeetingTime Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 328 Accepted Submission(s): 99 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. |
思路:点 连所有 它从属的块,块存储它所有的点。从1和n各跑一次最短路,然后枚举相遇点,更新答案。
用SPFA写了一发,TLE o(╯□╰)o 改了下又WA了,太弱了。
不得不膜拜大牛了,最后发现是用优先队列 dijkstraAC的。 (⊙o⊙)哦
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN 100000+10 #define MAXM 50000000 #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) using namespace std; struct Node{ int d, node; bool friend operator < (Node a, Node b){ return a.d > b.d; } }; vector<int> B[MAXN]; vector<int> G[MAXN]; int n, m; int T[MAXN]; void getMap() { Ri(n), Ri(m); for(int i = 1; i <= n; i++) G[i].clear(); for(int i = 1; i <= m; i++) { int num; Ri(T[i]), Ri(num); B[i].clear(); while(num--) { int a; Ri(a); G[a].push_back(i); B[i].push_back(a); } } } int dist[2][MAXN]; bool vis[MAXN]; bool block[MAXN]; void SPFA(int s, int op) { priority_queue<Node> Q; CLR(dist[op], INF); CLR(vis, false); CLR(block, false); dist[op][s] = 0; Q.push((Node){dist[op][s], s}); while(!Q.empty()) { int u = Q.top().node; Q.pop(); if(block[u]) continue; block[u] = true; for(int i = 0; i < G[u].size(); i++) { int k = G[u][i]; if(vis[k]) continue; vis[k] = true; for(int j = 0; j < B[k].size(); j++) { int v = B[k][j]; if(dist[op][v] > dist[op][u] + T[k]) { dist[op][v] = dist[op][u] + T[k]; Q.push((Node){dist[op][v], v}); } } } } } vector<int> P; int kcase = 1; void solve() { getMap(); SPFA(1, 0); SPFA(n, 1); int ans = INF; for(int i = 1; i <= n; i++) { int need = max(dist[0][i], dist[1][i]); if(ans > need) { ans = need; P.clear(); } if(ans == need) P.push_back(i); } printf("Case #%d: ", kcase++); if(ans == INF) printf("Evil John\n"); else { printf("%d\n", ans); int top = P.size()-1; for(int i = 0; i < top; i++) printf("%d ", P[i]); printf("%d\n", P[top]); } } int main() { int t; Ri(t); W(t){ solve(); } return 0; }
相关文章推荐
- 语义分析的一些方法(下篇)
- 树---判断两棵树是否相等
- Codevs P1982 加密算法
- hdu1241 Oil Deposits
- [leetcode 198]House Robber
- VM VirtualBox虚拟机共享文件夹(ubuntu)
- 浅谈C#集合【转】
- 【问题解决】线程间操作无效:从不是创建控件“textBox1”的线程访问它
- U3D 背景音效和事件触发音效
- 阅读有关软件工程与计算机科学区别的文章,谈谈你的看法。
- bzoj1620【Usaco2008 Nov】Time Management 时间管理
- 玩转树莓派--网卡配置
- 简单逆向
- 物化视图的刷新(转载)
- 构造方法
- LeetCode Count Complete Tree Nodes 二叉树
- 不同的场景使用不同的泛型通配符
- jenkins git插件安装
- redis主从设置
- hdu2844Coins【多重背包】