[Dijkstra] 集合内点可达求最短路 HDU5521

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5524    Accepted Submission(s): 1751


 

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.

 

 

Source

2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

 

 

每个集合对应一个虚点
集合里每个元素指向虚点长度为 w
虚点指向集合里每个元素长度为0
从 1 和 n 出发各跑一次最短路

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mn = 1e5 + 5;
const long long inf = 1e18;

int n, m;

int cnt;
int fr[2 * mn], to[40 * mn], nx[40 * mn];
ll cost[40 * mn];
void add_edge(int s, int t, ll w)
{
	cost[cnt] = w;
	to[cnt] = t;
	nx[cnt] = fr[s];
	fr[s] = cnt++;
}

bool vis[2 * mn];
ll dis1[2 * mn], disn[2 * mn];
struct qnode
{
	int id;
	ll dist;
};
bool operator <(const qnode &a, const qnode &b)
{
	return a.dist > b.dist;
}
void dijkstra(int s, ll dis[])
{
	memset(vis, 0, sizeof vis);
	for (int i = 1; i <= m + n; i++)
		dis[i] = inf;
	
	priority_queue<qnode> que;
	dis[s] = 0;
	qnode q;
	q.id = s, q.dist = 0;
	que.push(q);
	
	while (!que.empty())
	{
		qnode tmp = que.top();
		que.pop();
		if (vis[tmp.id])
			continue;
		vis[tmp.id] = 1;
		for (int i = fr[tmp.id]; i != -1; i = nx[i])
		{
			if (!vis[to[i]] && dis[to[i]] > dis[tmp.id] + cost[i])
			{
				dis[to[i]] = dis[tmp.id] + cost[i];
				qnode p;
				p.id = to[i], p.dist = dis[to[i]];
				que.push(p);
			}
		}
	}
}

int pans[mn];
int main()
{
	int T;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++)
	{
		cnt = 0;
		memset(fr, -1, sizeof fr);
		
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= m; i++)
		{
			ll w;
			int num;
			scanf("%lld %d", &w, &num);
			while (num--)
			{
				int s;
				scanf("%d", &s);
				add_edge(s, i + n, w);
				add_edge(i + n , s, 0);
			}
		}
		
		dijkstra(1, dis1);
		dijkstra(n, disn);
		
		int num = 0;
		ll ans = inf;
		for (int i = 1; i <= n; i++)
		{
			ll ma = max(dis1[i], disn[i]);
			if (ma < ans)
			{
				ans = ma;
				num = 0;
			}
			if (ma == ans)
				pans[++num] = i;
		}
		sort(pans + 1, pans + num + 1);
		
		if (ans == inf)
		{
			printf("Case #%d: Evil John\n", cas);
			continue;
		}
		printf("Case #%d: %lld\n", cas, ans);
		for (int i = 1; i < num; i++)
			printf("%d ", pans[i]);
		printf("%d\n", pans[num]);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值