#HDU 3873 Invade the Mars(dijkstra变体, 带限制最短路)

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

Hint

The Map is like this: We can follow these ways to achieve the fastest speed: 1->2->3,1->2->5,1->4->6.

题目大意 : 起点为1, 终点为n, 每个城市可能被其他城市保护着, 到达某一城市的最短距离等于到达该城市的距离和到达每个保护他城市的距离的最大值 ,输出最短路

思路 : 两个月前写这题,想了一个下午没写出来,今天终于A了哈哈。想明白了其实很简单,改一下dijkstra入队的条件就好, 我们将每个城市的受保护量给存下来, 再将每个城市保护的城市存下来, (这两个不一样),跑最短路的过程中, 当碰到一个城市不受其他城市保护, 就将该城市入队,第一个城市一定没有保护, 所以第一个直接入队,然后看下第一个城市保护着哪些城市, 将到这些城市的距离和到目前该点的距离比较,取最大值,并使受保护量 - 1, 在比较大小的过程中,加一个不受保护的判断条件就可以了

AC代码 :

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
const ll INF = 0x3f3f3f3f;

struct node
{
	int v;
	ll w;
	int next;
}e[MAXN];
struct edge
{
	int id;
	ll w;
	bool operator < (const edge &oth) const
	{
		return w > oth.w;
	}
}mid;
int head[MAXN], c[MAXN], n, m, cnt, T;
ll dis[MAXN];
vector <int> p[MAXN];
void init() {
	memset(c, 0, sizeof(c));
	memset(head, -1, sizeof(head));
	for (int i = 0; i <= n; i++) p[i].clear();
	cnt = 0;
}
void add(int from, int to, ll dis) {
	e[++cnt].v = to;
	e[cnt].w = dis;
	e[cnt].next = head[from];
	head[from] = cnt;
}
void dijkstra(int u) {
	priority_queue <edge> q;
	memset(dis, INF, sizeof(dis));
	dis[u] = 0;
	q.push({ u, 0 });
	while (!q.empty()) {
		mid = q.top();
		q.pop();
		int ans = mid.id;
		if (mid.w != dis[ans]) continue;
		for (int i = 0; i < p[ans].size(); i++) { //到达某个城市的最值更新
			dis[p[ans][i]] = max(dis[ans], dis[p[ans][i]]);
			if (!--c[p[ans][i]]) q.push({ p[ans][i], dis[p[ans][i]] });
		} // 若不受保护,则入队
		for (int i = head[ans]; i != -1; i = e[i].next) {
			if (dis[e[i].v] > dis[ans] + e[i].w) {
				dis[e[i].v] = dis[ans] + e[i].w;
				if (!c[e[i].v]) q.push({ e[i].v, dis[e[i].v] });
			}
		}
	}
}

int main()
{
	cin >> T;
	while (T--) {
		scanf("%d%d", &n, &m);
		init();
		for (int i = 0; i < m; i++) {
			int ui, vi;
			ll wi;
			scanf("%d%d%lld", &ui, &vi, &wi);
			add(ui, vi, wi);
		}
		int k, v;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &k);
			c[i] = k;  // 受多少城市保护
			while (k--) { // 谁保护着谁
				scanf("%d", &v);
				p[v].push_back(i);
			}
		}
		dijkstra(1);
		cout << dis[n] << endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值