hdu 3873 Invade the Mars(heap + dij变形)

思路:题目意思很简单,就是说如果没有攻占保护x的城市,就不能攻占,我们可以用pro[x]记录保护x的所有城市被攻占的最早时间,那么能到x的最短时间为pro[x]和到达x的最短路中的较大者 .dij入队过程中只把In[x](没有被包含的城市)入队 对于出队的x,它的最短时间已经确定,表示已经被占领,它所保护的城市的保护度减 1,一旦某个被保护的城市的保护度为零且已经到底(未占领,d[x]!=inf),就可以确定到达它的 最短时间(为max(pro[x],dist[x])),它也就到了入队的时机。

具体参考:点击打开链接

自己试了一发spfa,发现有个地方很难处理掉,即被保护的点是最后才能访问,但是队列中之前已经被访问过了(就是后来能访问的时候已经无法访问到)。然后卡卡卡...

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;

typedef __int64 ll;
typedef pair<ll, int> Pair;
const int N = 3005;
const int M = 70005;
const ll inf = 0x3f3f3f3f;

struct node{
	int v;
	ll w;
};

int vis[N];
ll dis[N];
int t[N];
ll low[N];
vector<int> vv[N];
vector<node> a[N];

int m, n;
void init()
{
	for( int i = 1; i <= n; i++ )
		vv[i].clear(), a[i].clear();
}

void dijstra()
{
	priority_queue<Pair, vector<Pair>, greater<Pair> >q;
	while( !q.empty() )
		q.pop();
	for( int i = 1; i <= n; i++ )
	{
		dis[i] = inf;
		vis[i] = 0;
		low[i] = 0;
	}
	dis[1] = 0;
	q.push(make_pair(dis[1], 1));
	while( !q.empty() )
	{
		Pair pp = q.top();
		q.pop();
		int u = pp.second;
		if( vis[u] )
			continue;
		vis[u] = 1;
		for( int i = 0; i < vv[u].size(); i++ )
		{
			int to = vv[u][i];
			t[to]--;
			low[to] = max(low[to], dis[u]);
			if( dis[to] != inf && t[to] == 0 )
			{
				dis[to] = max( dis[to], low[to] );
				q.push(make_pair(dis[to], to));
			}
		}
		for( int i = 0; i < a[u].size(); i++ )
		{
			int to = a[u][i].v;
			int w = a[u][i].w;
			if( dis[to] > dis[u] + w )
			{
				dis[to] = max(dis[u] + w, low[to]);
				if( t[to] == 0 )
				{
					q.push(make_pair(dis[to], to));
				}
			}
		}
	}
}

int main()
{
	int tot;
	scanf("%d", &tot);
	while(tot--)
	{
		scanf("%d%d", &n, &m);
		init();
		int u, v, w, x;
		while(m--)
		{
			scanf("%d%d%d", &u, &v, &w);
			node tmp;
			tmp.v = v;
			tmp.w = w;
			a[u].push_back(tmp);
		}
		for( int i = 1; i <= n; i++ )
		{
			scanf("%d", &t[i]);//达成条件的点数目 
			for( int j = 1; j <= t[i]; j++ )
			{
				scanf("%d", &x);
				vv[x].push_back(i);
			}
		}
		dijstra();
		printf("%d\n", dis[n]);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值