最短路:HDU3873

 

Invade the Mars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 365768/165536 K (Java/Others)
Total Submission(s): 2862    Accepted Submission(s): 821


 

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

 

这题的思路有一点绕,一开始想的是先dijkstra然后回溯最短路上需要经过的点计算每个点的最大时间,再修改最终时间,然后发现这个想法很有问题,然后就绕进去了。然后以下的正确思路是,在dijkstra的过程中维护每一个点所需的最大时间。pro[u][v]表示u点需要保护的点为 v,is[u]表示 u 有几个保护它的点,time[u]表示攻占 u 点的最大时间,因此在dijkstra中,若是一个点 u 已被攻占,则is[u]--, 而只有当保护 u 的所有点都已被攻占,u 才能入队。之后就是dijkstra的常规操作。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN=3005;
const long long INF=0x3f3f3f3f;
int n, m;
struct qnode{
	int v;
	int w;
	qnode(int _v=0,int _w = 0):v(_v),w(_w){}
	bool operator <(const qnode &r)const{
		return w > r.w;
	}
};
struct Edge{
	int v;
	int w;
	Edge(int _v=0,int _w = 0):v(_v),w(_w){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
vector<int> pro[MAXN];
int is[MAXN];
int time[MAXN];
void Dijkstra(int n,int start){
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++) dist[i]=INF;
	priority_queue<qnode>que;
	while(!que.empty()) que.pop();
	dist[start]=0;
	que.push(qnode(start,0));
	qnode tmp;
	while(!que.empty()){
		tmp=que.top();
		que.pop();
		int u=tmp.v;
		if(vis[u]) continue;
		vis[u]=true;
		for(int i = 0; i < pro[u].size(); i++)
		{
			int v = pro[u][i];
			is[v]--;
			time[v] = max(time[v], dist[u]);
			if(dist[v] != INF && !is[v])
			{
				dist[v] = max(dist[v], time[v]);
				que.push(qnode(v, dist[v]));
			}
		}
		for(int i = 0; i < E[u].size(); i++){  
			int v = E[u][i].v;
			if(dist[v] > dist[u] + E[u][i].w)
			{
				dist[v] = dist[u] + E[u][i].w;
				dist[v] = max(dist[v], time[v]);
				if(!is[v])
					que.push(qnode(v,dist[v]));
			}
		}
	}
}
void addedge(int u,int v,int w){
	E[u].push_back(Edge(v,w));
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		memset(is, 0, sizeof(is));
		memset(time, 0, sizeof(time));
		for(int i = 1; i <= n; i++)
		{
			pro[i].clear();
			E[i].clear();
		}
		for(int i = 0; i < m; i++)
		{
			int u, v, c;
			scanf("%d%d%d", &u, &v, &c);
			addedge(u, v, c);
		}
		for(int i = 1; i <= n; i++)
		{
			int num;
			scanf("%d", &is[i]);
			for(int j = 0; j < is[i]; j++)
			{
				int v;
				scanf("%d", &v);
				pro[v].push_back(i);
			}
		}
		Dijkstra(n, 1);
		printf("%d\n", dist[n]);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值