hdu3873(dij + top排序

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/iamhpp/article/details/102764788

Problem Description

It’s now the year 21XX,when the earth will explode soon.The evil U.S. decided to invade the Mars to save their lives.
But the childlike Marsmen never keeps any army,because war never take place on the Mars.So it’s very convenient for the U.S. to act the action.
Luckily,the Marsmen find out the evil plan before the invadation,so they formed a defense system.The system provides enchantment for some citys,and the enchantment generator for city A maybe set in city B,and to make things worse,both city B and C and more will provide echantment for city A.
The satelite of U.S. has got the map of the Mars.And they knows that when they enter a city,they can destory all echantment generator in this city at once,and they can enter a city only if they has destoryed all enchantment generator for this city,but troops can stay at the outside of the city and can enter it at the moment its echantment is destoryed.Of course the U.S. army will face no resistance because the Mars keep no army,so troops can invade in many way at the same time.
Now the U.S. will invade the Mars,give you the map,your task is to calculate the minimium time to enter the capital of the Mars.

Input

The first line contains an integer T,which is the number of test cases.
For each testcase:
The first line contains two integers N and M,1<=N<=3000,1<=M<=70000,the cities is numbered from 1 to N and the U.S. landed on city 1 while the capital of the Mars is city N.
The next M lines describes M paths on the Mars.Each line contains three integers ai,bi and wi,indicates there is a unidirectional path form ai to bi lasts wi minutes(1<=wi<=10^8).
The next N lines describes N citys,the 1+M+i line starts with a integer li,followed with li integers, which is the number of cities has a echantment generator protects city i.
It’s guaranteed that the city N will be always reachable.

Output

For each case,print a line with a number indicating the minimium time needed to enter the capital of the Mars.

Sample Input

1
6 6
1 2 1
1 4 3
2 3 1
2 5 2
4 6 2
5 3 2
0
0
0
1 3
0
2 3 5

Sample Output

5

分析

解锁一个点的最小时间取决于两点:
到这个点的最短路长度,到保护这个点的最短路长度,对这两个取 m a x max 就可以得到最小时间。得到最小时间后再入队。
于是我们就跑最短路的时候进行 t o p top 排序就可以了,入度为 0 0 就入队。
这里要用dij,因为我们必须保证枚举到 a a 时, a a 一定已经求出了最短路。

代码如下

#include <bits/stdc++.h>
#define M 70005
#define N 3005
#define LL long long
#define inf 2000000000
using namespace std;
struct node{
	int a, b, c, n;
	bool operator < (const node & A) const{
		return c > A.c;
	}
}d[M];
int h[N], cnt, in[N];
LL dis[N], mt[N];
vector<int> vec[N];
void cr(int a, int b, int c){
	d[++cnt].a = a; d[cnt].b = b; d[cnt].c = c; d[cnt].n = h[a]; h[a] = cnt;
}
void dij(){
	int i, a, b, c;
	node t;
	priority_queue<node> q;
	memset(dis, 127, sizeof(dis));
	t.a = 1;
	t.c = dis[1] = 0;
	q.push(t);
	while(!q.empty()){
		t = q.top(); q.pop();
		a = t.a;
		//printf("======%d %d %d\n", a, dis[a], t.c);
		if(t.c != dis[a]) continue;
		for(i = 0; i < vec[a].size(); i++){
			b = vec[a][i];
			in[b]--;
			mt[b] = max(dis[a], mt[b]);
			if(dis[b] < inf && !in[b]){
				dis[b] = max(dis[b], mt[b]);
				t.a = b; t.c = dis[b];
				q.push(t);
			}
		}
		for(i = h[a]; i; i = d[i].n){
			b = d[i].b;
			c = d[i].c;
			if(dis[b] > dis[a] + c){
				dis[b] = dis[a] + c;
				if(!in[b]){
					t.a = b; t.c= dis[b];
					q.push(t);
				} 
			}
		}
	}
}
int main(){
	int i, j, n, m, T, a, b, c;
	scanf("%d", &T);
	while(T--){
		memset(in, 0, sizeof(in));
		memset(h, 0, sizeof(h)); 
		memset(mt, 0, sizeof(mt));
		cnt = 0;
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n; i++) vec[i].clear();
		for(i = 1; i <= m; i++){
			scanf("%d%d%d", &a, &b, &c);
			cr(a, b, c);
		}
		for(i = 1; i <= n; i++){
			scanf("%d", &in[i]);
			for(j = 1; j <= in[i]; j++){
				scanf("%d", &c);
				vec[c].push_back(i);
			}
		}
		dij();
		printf("%d\n", dis[n]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/iamhpp/article/details/102764788
top
今日推荐