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.
题意:
有个农场有个n块田地编号为1~n.然而这n块田地被栅栏分为m个集合,集合i有若干块土地表明,这些土地块之间可以相互到达且时间代价为ti。现在一个人在土地块1,一个人在土地块n,他们两人想选择某块土地开会,要求前往的时间花费最少。输出最短时间和开会土地块编号。如果有多个最佳答案在输出升序.(注意:同一块土地可能属于多个集合也可以不属于任何集合)
分析:
因为Σs 太过巨大,如果强行建边的话直接爆掉,所以需要想个办法建出等效图。这里给个方案,对于每个集合新添加一个节点,建立集合每个点到这个节点的有向边代价为0。然后建立这个点到集合每个点的有向边,代价为ti。这样一个集合内的完全图就被转化了。新图的边数只有节点数*2.那么总边数就是Σs*2。图建立起来就好办了,从1和n分别跑bfs就可以算出1和n到每个点的最短距离然后就可以找到最小消耗的点了。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const ll inf = 1e17;
const int maxm = 1000010;
const int maxn = 100010;
int to[2*(maxm+maxn)],fn[maxm+maxn],nt[2*(maxm+maxn)],tot;
int cost[2*(maxm+maxn)];
ll ans1[maxn],ans2[maxn];
int n,m;
void addedge(int u,int v,ll c)
{
to[tot] = v;
cost[tot] = c;
nt[tot] = fn[u];
fn[u] = tot++;
}
struct edge
{
int p;
ll len;
edge(){}
edge(int tp,ll l){ p = tp;len = l;}
bool operator < (const edge& other)const
{
return len > other.len;
}
};
priority_queue<edge>team;
bool visit[maxn+maxm];
bool use[maxn+maxm];
void bfs(int start,ll ans[])
{
while(team.size())team.pop();
team.push(edge(start,0));
memset(visit,0,sizeof(visit));
while(team.size())
{
edge now = team.top();
team.pop();
if(visit[now.p] == 0)
{
if(now.p <= n)
ans[now.p] = now.len;
visit[now.p] = 1;
}
else continue;
for(int i = fn[now.p] ; i != -1 ; i = nt[i])
{
if(visit[to[i]]) continue;
team.push(edge(to[i],cost[i]+now.len));
}
}
}
int main()
{
int t,_case = 0;
scanf("%d",&t);
while(t--)
{
tot = 0;
scanf("%d%d",&n,&m);
ll ti;
int k,v;
memset(fn,-1,sizeof(fn));
for(int i = 1 ; i <= m ; i ++)
{
scanf("%lld%d",&ti,&k);
for(int j = 1 ; j <= k ; j ++)
{
scanf("%d",&v);
addedge(v,n+i,0);
addedge(n+i,v,ti);
}
}
for(int i = 0 ; i <= n+m ; i ++)
ans1[i] = ans2[i] = inf;
bfs(1,ans1);
bfs(n,ans2);
ll ans = inf;
int flag = 0;
for(int i = 1 ; i <= n ; i ++)
{
if(max(ans1[i],ans2[i]) < ans) ans = max(ans1[i],ans2[i]);
}
printf("Case #%d: ",++_case);
if(ans != inf)
{
printf("%lld\n",ans);
int flag = 0;
for(int i = 1 ; i <= n ; i ++)
{
if(max(ans1[i],ans2[i]) == ans)
{
if(flag == 0)
{
printf("%d",i);
flag = 1;
}
else
{
printf(" %d",i);
}
}
}
printf("\n");
}
else
printf("Evil John\n");
}
return 0;
}