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
居然在知道思路而且代码不难实现的情况下依旧自己没做出来QAQ
题意:给定n个点组成m个有重叠的团,同一团内两两点之间的时间给定且一致,Bessie lives in the first block while Elsie lives in the n -th one,求两个人见面的时间和可选位置
给出的数据微微大了一些不可以团内两两加边,那就每个团虚拟出来一个点,给定的边权就变成了团内点都与这个点相连,开始考虑到边权大小的问题,想加1/2的边权,出现小数,不好不好,最终结果除以2就好啦。选点的话很容易想到是让1.n作为原始的点分别跑一遍最短路,spfa记得要用队列,栈会超时!!
自己wa在哪里了呢?不是说dis[] dis1[]出现不可达的状态就不行,又不是所有点都必须经过,有不是正无穷的而且能走到就可以了嘛
#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=1500000;
int head[maxn];
long long dis[maxn],dis1[maxn],maxlen[maxn];
int n,m,sum;
bool vis[maxn];
long long max(long long a,long long b){return a>b?a:b;}
struct node{
int v,w,next;
}edge[2500100];
void addedge(int a,int b,int c){
edge[sum].v=b;
edge[sum].w=c;
edge[sum].next=head[a];
head[a]=sum++;
}
int outque[maxn],top;
bool spfa(int s){
top=0;
for(int i=0;i<=n+m;i++)dis[i]=pow(10,18)+7;
memset(vis,0,sizeof(vis));
memset(outque,0,sizeof(outque));
queue<int>Q;
Q.push(s);
vis[s]=1;//vis数组的作用不是判断它是否被更新过 而是是否在栈里!
dis[s]=0;
while(!Q.empty()){
int tmp=Q.front();
Q.pop();
vis[tmp]=0;
outque[tmp]++;
// if(outque[tmp]>n) return 0; //判断负环,当然这里没有必要写它
int k=head[tmp];
while(k>-1){
if(dis[edge[k].v]>edge[k].w+dis[tmp]){
dis[edge[k].v]=edge[k].w+dis[tmp];
if(vis[edge[k].v]==0){
vis[edge[k].v]=1;
Q.push(edge[k].v);
}
}
k=edge[k].next;
}
}
return 1;
}
int main()
{
//freopen("cin.txt","r",stdin);
int t,s,a,cas=1;
int time;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
sum=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&time,&s);
while(s--)
{
scanf("%d",&a);
if(a==0)continue;
addedge(a,n+i,time);
addedge(n+i,a,time);
}
}
printf("Case #%d: ",cas++);
if(spfa(1))
{
for(int i=1;i<=n;i++)dis1[i]=dis[i];//printf("i=%d,dis=%lld,dis1=%lld\n",i,dis[i],dis1[i]);
// for(int i=1;i<=n;i++)printf("i=%d,dis=%lld,dis1=%lld\n",i,dis[i],dis1[i]);
spfa(n);
bool fl=0;
long long tmp1=pow(10,18);
long long maxlength=tmp1;
int tmp=0;
for(int i=1;i<=n;i++)
{
// printf("i=%d,dis=%lld,dis1=%lld,",i,dis[i],dis1[i]);
// if(dis[i]>=tmp1||dis1[i]>=tmp1)fl=1;
maxlen[i]=max(dis1[i],dis[i]);
if(maxlen[i]<maxlength)
{
maxlength=maxlen[i];
tmp=i;
}
// printf("maxlen=%lld,maxlength=%lld\n",maxlen[i],maxlength);
}
if(maxlength==tmp1)
{
printf("Evil John\n");
continue;
}
printf("%I64d\n",maxlength/2);
for(int i=1;i<=n;i++)
if(maxlen[i]==maxlength)
{
if(i==tmp)printf("%d",i);
else printf(" %d",i);
}
puts("");
}
else
printf("Evil John\n");
}
return 0;
}