您的位置:首页 > 其它

hdoj 5521 Meeting 【优先队列 dijkstra】

2015-11-02 21:46 330 查看

Meeting

Time 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.

 

题意:给定n个点、m个块,给你组成每个块的点,已知块中任意两点间互达需要花费的时间为定值。现在一人从点1出发,一人从点n出发,问你两人相遇的所需的最短时间,不能相遇输出Evil John,否则输出相遇所需的最短时间和相遇点,若有多个相遇点,按字典序输出。

思路:点 连所有 它从属的块,块存储它所有的点。从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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: