Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3813 Accepted Submission(s): 1226
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.
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 .
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.
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 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
然后A在点1,B在点n,然后让你找到一个点,使得max(disA[i],disB[i])最小
如果有多个答案,按照字典序输出所有答案
如果没有答案输出Evil John
题解报告:考虑到m个集合,集合内两两互相连接显然建边的话会超出内存。我们可以让每个集合看做一个点,集合内所有的点都与他相连,求出最短路除以二就好
代码来源Stone
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;
const int N = 1e6 + 10;
const long long INF = 1e17+10;
struct Node
{
int next;
int to;
long long w;//有权值的话,这一句启用
}edge[N*4];
int vis[N];
long long book[N];
long long dis[2][N];
int head[N];
int vertexs;
int n, m;
int iTime, number, soloPoint;
int cnt;
void add(int u, int v, long long w)//存一条以u为起点,v为终点,w为边权值的有向边。
{
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
memset(edge, 0, sizeof(edge));
}
long long dijkstraListHeap(int beginX, int endX, int index)
{
int minVertex;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > heap;//一个最小堆
//初始化
for(int i=1;i<=vertexs;i++){
dis[index][i] = INF;//起始距离初始化为INF
vis[i] = 0;
}
dis[index][beginX] = 0;
heap.push(make_pair(dis[index][beginX], beginX));//开始的点入队
while(!heap.empty()){
minVertex = heap.top().second;
heap.pop();
if(vis[minVertex]) continue;//如果这个点之前没有被访问过
vis[minVertex] = 1;
int u = minVertex;
for(int i=head[u];~i;i=edge[i].next){//遍历最小点所连的所有边
int v = edge[i].to;
long long w = edge[i].w;
if(!vis[v]){//如果这个点未被访问过
if(dis[index][v] > dis[index][u] + w){
dis[index][v] = dis[index][u] + w;
heap.push(make_pair(dis[index][v], v));//如果路径可以变小则压进去
}
}
}
}
return dis[index][endX];
}
int main(void)
{
int T;
scanf("%d", &T);
for(int cas=1;cas<=T;cas++){
init();
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++){
scanf("%d%d", &iTime, &number);
for(int j=1;j<=number;j++){
scanf("%d", &soloPoint);
int temp = n + i;
add(temp, soloPoint, iTime);
add(soloPoint, temp, iTime);
}
}
vertexs = m+n;
dijkstraListHeap(1, vertexs, 0);
dijkstraListHeap(n, vertexs, 1);
long long iMin = INF;
int flag = 0;
for(int i=1;i<=n;i++){
if(iMin > max(dis[0][i]/2, dis[1][i]/2)){
flag = 1;
iMin = max(dis[0][i]/2, dis[1][i]/2);
book[i] = iMin;
}else if(iMin == max(dis[0][i]/2, dis[1][i]/2)) {
book[i] = iMin;
flag++;
}
}
if(iMin >= INF/2){
printf("Case #%d: Evil John\n", cas);
}else{
printf("Case #%d: %lld\n", cas, iMin);
for(int i=1;i<=n;i++){
if(book[i] == iMin){
if(flag != 1){
printf("%d ", i);
flag--;
}else{
printf("%d\n", i);
flag--;
}
}
}
}
}
return 0;
}