欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU5521最短路径

程序员文章站 2022-05-22 14:36:08
...

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)


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 (1im) 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 (1T6), the number of test cases. Then T test cases
follow.

The first line of input contains n and m2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109) and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
 

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个结点,有m个区域,相同的点可以在不同的区域中,同一区域内的点之间的距离都是一样的。有两个人分别在结点1和结点n,让两个人选择一个地方相聚,到达这个地方所用的时间最少。输出最少的时间,以及所有最短时间的点。

分析:读完题后,发现这是个图论,更具体一点是最短路径。但这是个最短路径的变形,需要从两个点都进行一遍求最短路径,将时间保存在两个数组中,然后处理数组。

最短路径使用Dijkstra来求,要结合优先级队列来求,因为点的数目太多。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define N 100005
#define INF 1e15
typedef long long ll;
vector<int> E[N];//区域包含的结点 
vector<int> V[N];//结点所属区域 
ll S[N];//结点内时间 
ll dis1[N], dis2[N];
bool viss[N];//区域是否松弛过 
bool vis[N];
int n, m;
struct node{
	int x;
	ll dis;
	bool operator <(const node &a)const{
		return a.dis < dis;
	}
};
priority_queue<node> Q;
void init(ll dis[]){
	if(!Q.empty())Q.pop();
	for(int i = 1; i <= n; i++)
		dis[i] = INF, vis[i] = false;
	for(int i = 0; i < n; i ++)
		vis[i] = false;
	for(int i = 0; i < m; i++)
		viss[i] = false;
}
void Dijkstra(int st, ll dis[]){
	init(dis);
	node p, q;
	p.x = st, p.dis = 0;
	Q.push(p);
	dis[st] = 0;
	while(!Q.empty()){
		p = Q.top();
		Q.pop();
		if(vis[p.x])continue;
		vis[p.x] = true;
		for(int i = 0; i < V[p.x].size(); i++){
			int e = V[p.x].at(i);
			if(viss[e])continue;
			viss[e] = true;
			for(int j = 0; j < E[e].size(); j++){
				int x = E[e].at(j);
				if(dis[x] > dis[p.x] + S[e]){
					dis[x] = dis[p.x] + S[e];
					q.x = x;
					q.dis = dis[x];
					Q.push(q);
				}
			}
		}
		
	}
}
void solve(){
	Dijkstra(1, dis1);
	Dijkstra(n, dis2);
	ll Min = INF;
	for(int i = 1; i <= n; i ++)
		Min = min(Min, max(dis1[i], dis2[i]));
	if(Min == INF)
		printf("Evil John\n");
	else{
		printf("%d\n", Min);
		int flag = 0;
		for(int i = 1; i <= n; i++){
			if(max(dis1[i], dis2[i]) == Min){
				if(flag)printf(" ");
				else flag = 1;
				printf("%d", i);
			}
		}
		printf("\n");
	}
}
int main(){
	int t, cas = 0;
	int cnt;
	cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i = 1; i <= n; i++)V[i].clear();
		for(int i = 0; i < m; i ++){
			scanf("%d%d", &S[i], &cnt);
			E[i].clear();
			int x;
			for(int j = 0; j < cnt; j ++){
				scanf("%d", &x);
				E[i].push_back(x);
				V[x].push_back(i);
			}
		}
		printf("Case #%d: ", ++cas);
		solve();
	}
	return 0;
}

当时没做出来,只能怪自己太弱,还需努力。


上一篇: 图论模板

下一篇: 图论模板