[题解] [SDOI2010] 大陆争霸

题面

题解

对于到某个点\(i\), 我们有两个条件

到达\(i\)点的最短时间, 用\(dis1_i\)表示

破坏完所有保护\(i\)点的城市的最小时间

两者取\(max\)即到\(i\)点的最小时间

对于破坏某个城市的保护点, 用类似于拓扑序的方式处理即可

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define N 200005
using namespace std;

int n, m, in[N], dis1[N], dis2[N]; 
struct edge { int to, next, cost; }; 
struct dist { int num, dis; bool operator < (const dist &p) const { return dis > p.dis; } }; 
struct Graph
{
	edge e[N << 1]; int head[N]; int cnt;
	Graph() { cnt = 0; }
	inline void adde(int u, int v, int w) { e[++cnt] = (edge) { v, head[u], w }; head[u] = cnt; } 
} A, B; 
bool vis[N]; 

namespace Heap
{
	dist heap[N << 2]; int sz = 0;
	void push(dist x) { heap[++sz] = x; push_heap(heap + 1, heap + sz + 1); }
	void pop() { pop_heap(heap + 1, heap + sz + 1); sz--; }
	dist top() { return heap[1]; }
	bool empty() { return !sz; }
};

using namespace :: Heap; 

inline int read()
{
	int x = 0, w = 1; char c = getchar();
	while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
	while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
	return x * w;
}

void dijkstra()
{
	memset(dis1, 0x3f, sizeof(dis1));
	memset(vis, 0, sizeof(vis)); 
	dis1[1] = 0; push((dist) { 1, 0 }); 
	while(!empty())
	{
		dist tmp = top(); int u = tmp.num; pop(); 
		if(vis[u]) continue; vis[u] = 1; 
		for(int i = A.head[u]; i; i = A.e[i].next)
		{
			int v = A.e[i].to; 
			if(dis1[v] > tmp.dis + A.e[i].cost && !vis[v])
			{
				dis1[v] = tmp.dis + A.e[i].cost; 
				if(!in[v]) push((dist) { v, max(dis1[v], dis2[v]) }); 
			}
		}
		for(int i = B.head[u]; i; i = B.e[i].next)
		{
			int v = B.e[i].to; in[v]--; 
			dis2[v] = max(dis2[v], tmp.dis); 
			if(!in[v]) push((dist) { v, max(dis1[v], dis2[v]) }); 
		}
	}
}

int main()
{
	n = read(); m = read();
	for(int i = 1; i <= m; i++)
	{
		int u = read(), v = read(), w = reaD();
		A.adde(u, v, w); 
	}
	for(int i = 1; i <= n; i++)
	{
		in[i] = reaD();
		for(int j = 1; j <= in[i]; j++)
		{
			int x = read();
			B.adde(x, i, 0); 
		}
	}
	dijkstra(); 
	printf("%d\n", max(dis1[n], dis2[n])); 
	return 0;
}
posted @ 2019-07-14 16:07  ztlztl  阅读(137)  评论(0编辑  收藏  举报