Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1358 Accepted Submission(s): 435
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.
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.
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.
由於每一個集合的點有很多,若集合兩兩之間連邊,邊數非常大,一開始這樣就超時了……然後正確做法是對每一個集合再虛擬一個節點,然後每一個點到這個節點的距離都是t且是雙向的,這樣一來這些點對於虛擬節點來說就都是等價的了,可以減少很多邊,然後起始點就是1,終點是n,但是中間經過的一些點是虛擬節點,可以減少耗時……最後的距離要除以2,因爲虛擬節點本身就不存在,在某兩個實際存在點之間來回本來就只能算一次,但是用了虛擬節點卻會算兩次……即a->b會變成a->c->b,多加了一次相同 邊權
代碼:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1100010,M=3000010;
struct edge
{
int to;
LL dx;
int pre;
}E[M];
int head[M],cnt;
LL d1[N],d2[N];
int ans[N];
inline LL MAX(const LL &a,const LL &b)
{
return b>a?b:a;
}
void add(int s,int t,LL d)
{
E[cnt].to=t;
E[cnt].dx=d;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void spfa(const int &s,LL d[])
{
typedef pair<LL,int> pli;
d[s]=0;
priority_queue<pli>Q;
Q.push(pli(-d[s],s));
while (!Q.empty())
{
int now=Q.top().second;
Q.pop();
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
LL w=E[i].dx;
if(d[v]>d[now]+w)
{
d[v]=d[now]+w;
Q.push(pli(-d[v],v));
}
}
}
}
void init()
{
MM(head,-1);
cnt=0;
MM(d1,INF);
MM(d2,INF);
MM(ans,0);
}
int main(void)
{
int tcase,n,m,i,j,k,x,c,t;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
init();
scanf("%d%d",&n,&m);
for (i=1; i<=m; ++i)
{
scanf("%d%d",&t,&c);
for (j=0; j<c; ++j)
{
scanf("%d",&x);
add(x,n+i,t);
add(n+i,x,t);
}
}
spfa(1,d1);
spfa(n,d2);
printf("Case #%d: ",q);
LL dx=INF;
for (i=1; i<=n; i++)
{
LL temp=MAX(d1[i],d2[i]);
if(temp<dx)
dx=temp;
}
if(dx==INF)
puts("Evil John");
else
{
printf("%I64d\n",dx>>1);
int cnt=0;
for (i=1; i<=n; i++)
if(MAX(d1[i],d2[i])==dx)
ans[cnt++]=i;
for (i=0; i<cnt; ++i)
printf("%d%s",ans[i],i==cnt-1?"\n":" ");
}
}
return 0;
}