HDU5521 2015ACM/ICPC亞洲區瀋陽站 Meeting (最短路&建虛擬點)

Meeting

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


 
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.

 

題目大意 :有N(1e5)個點,M個集合,集合中任意兩點之間的距離爲W(1e9),點U從1出發,點V從n出發,找到某些點,U和V到該點的距離的最大值最小,輸出距離並升序輸出滿足條件的所有點

解法:

完全圖直接暴力建邊肯定T,現在只需要構造一種圖,能夠代替完全圖。對於邊權相等的完全圖,兩點之間的最短路一定只有一條邊,那麼建立一個虛擬點,來代替那個中間的“橋樑”就好,集合S的每個點到虛擬點的距離爲0,虛擬點到集合S的每個點距離爲W,就可以滿足啦。

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <ll, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 3e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

vector <pir> G[N << 1];
ll d1[N], d2[N];
bool vis[N];
int n, m;

void init() {
	for (int i = 1; i <= 3 * n; i++)
		G[i].clear();
	MEM(d1, LINF), MEM(d2, LINF);
}
void dijkstra(int x, ll *dis) {
	priority_queue <pir, vector <pir>, greater <pir>> q;
	MEM(vis, 0);
	q.push({ 0ll, x });
	dis[x] = 0;;
	while (!q.empty()) {
		pir now = q.top();
		q.pop();
		int u = now.second;
		if (vis[u])
			continue;
		vis[u] = true;
		for (auto it : G[u]) {
			int v = it.second; ll w = it.first;
			if (dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				q.push({ dis[v], v });
			}
		}
	}
}

int main()
{
	int T; cin >> T;
	int Case = 0;
	while (T--) {
		sc("%d %d", &n, &m);
		init();

		int cnt = 0;
		for (int i = 1; i <= m; i++) {
			int k, u; ll w;
			sc("%lld %d", &w, &k);
			cnt++;
			while (k--) {
				sc("%d", &u);   // 建邊
				G[n + cnt].push_back({ w, u });
				G[u].push_back({ 0, n + cnt });
			}
		}

		dijkstra(1, d1), dijkstra(n, d2);  // 正反最短路
		printf("Case #%d: ", ++Case);
		ll ans = LINF;
		for (int i = 1; i <= n; i++) 
			Min(ans, max(d1[i], d2[i]));
		if (ans == LINF)
			printf("Evil John\n");
		else {
			printf("%lld\n", ans);
			vector <int> v;
			for (int i = 1; i <= n; i++) {
				if (max(d1[i], d2[i]) == ans)
					v.push_back(i);
			}
			for (int i = 0; i < SZ(v) - 1; i++) // 注意輸出格式,末尾沒有空格
				printf("%d ", v[i]);
			printf("%d\n", v[SZ(v) - 1]);
		}
	}
	return 0;  // 改數組大小!!!用pair記得改宏定義!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章