P2446 [SDOI2010]大陆争霸

(color{#0066ff}{ 题目描述 })

幻想历8012年5月12日深夜,斯普林·布拉泽降下神谕:“Trust me, earn eternal life.”克里斯军团士气大增。作为克里斯军团的主帅,你决定利用这一机会发动奇袭,一举击败杰森国。具体地说,杰森国有N个城市,由M条单向道路连接。神谕镇是城市1而杰森国的首都是城市N。你只需摧毁位于杰森国首都的曾·布拉泽大神殿,杰森国的信仰,军队还有一切就都会土崩瓦解,灰飞烟灭。

为了尽量减小己方的消耗,你决定使用自爆机器人完成这一任务。唯一的困难是,杰森国的一部分城市有结界保护,不破坏掉结界就无法进入城市。而每个城市的结界都是由分布在其他城市中的一些结界发生器维持的,如果想进入某个城市,你就必须破坏掉维持这个城市结界的所有结界发生器。

现在你有无限多的自爆机器人,一旦进入了某个城市,自爆机器人可以瞬间引爆,破坏一个目标(结界发生器,或是杰森国大神殿),当然机器人本身也会一起被破坏。你需要知道:摧毁杰森国所需的最短时间。

(color{#0066ff}{输入格式})

输入文件的landcraft.in的第一行两个正整数N, M。

接下来M行,每行三个正整数ui, vi, wi,表示有一条从城市ui到城市vi的单向道路,自爆机器人通过这条道路需要wi的时间。

之后N行,每行描述一个城市。首先是一个正整数li,维持这个城市结界所使用的结界发生器数目。之后li个1~N之间的城市编号,表示每个结界发生器的位置。如果li = 0,则说明该城市没有结界保护,保证l1 = 0

(color{#0066ff}{输出格式})

输出文件landcraft.out仅包含一个正整数 ,击败杰森国所需的最短时间。

(color{#0066ff}{输入样例})

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

(color{#0066ff}{输出样例})

5

(color{#0066ff}{数据范围与提示})

对于20%的数据,满足N≤15,M≤50;

对于50%的数据,满足N≤500,M≤6,000;

对于100%的数据,满足N≤3,000,M≤70,000,1≤wi≤108。

输入数据保证一定有解,且不会存在维持某个城市结界的结界发生器在这个城市内部。

连接两个城市的道路可能不止一条,也可能存在一个城市自己到自己的道路。

(color{#0066ff}{ 题解 })

有条件的最短路

刚开始写的先正常跑一遍DIJ,然后倒着搜索,每个点的实际时间等于最短路根所有结界节点最短路的min

后来发现,当前点的最短路可能经过一些被保护的点,而这些点是没有算上去的

于是应该直接在最短路中处理

因为DIJ只能更新一次(为了保证复杂度)

所以一个点入堆当且仅当自己被所有结界城市和其他城市更新完后再入堆

记一个dis和一个实际距离,取max

#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
int n, m;
using std::pair;
using std::make_pair;
std::priority_queue<pair<int, int>, std::vector<pair<int, int> >, std::greater<pair<int, int> > > q;
struct node {
    int to, dis;
    node *nxt;
    node(int to = 0, int dis = 0, node *nxt = NULL): to(to), dis(dis), nxt(nxt) {}
    void *operator new(size_t) {
        static node *S = NULL, *T = NULL;
        return (S == T) && (T = (S = new node[1024]) + 1024), S++;
    }
};
const int inf = 0x7fffffff;
const int maxn = 5050;
int dis[maxn], f[maxn];
bool vis[maxn];
int du[maxn];
node *head[maxn], *h[maxn];
void add(int from, int to, int dis, node **hh) {
    hh[from] = new node(to, dis, hh[from]);
}
void dij() {
    for(int i = 1; i <= n; i++) dis[i] = inf;
    q.push(make_pair(dis[1] = 0, 1));
    while(!q.empty()) {
        int tp = q.top().second;
		q.pop();
		int val = std::max(dis[tp], f[tp]);
        if(vis[tp]) continue;
        vis[tp] = true;
        for(node *i = head[tp]; i; i = i->nxt) {
            if(dis[i->to] > val + i->dis) {
				dis[i->to] = val + i->dis;
				if(!du[i->to]) q.push(make_pair(std::max(f[i->to], dis[i->to]), i->to));
			}
        }
		for(node *i = h[tp]; i; i = i->nxt) {
			du[i->to]--;
			f[i->to] = std::max(f[i->to], val);
			if(!du[i->to]) q.push(make_pair(std::max(f[i->to], dis[i->to]), i->to));
		}
    }
}
int main() {
    n = in(), m = in();
    int x, y, z;
    for(int i = 1; i <= m; i++) {
        x = in(), y = in(), z = in();
        add(x, y, z, head);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = in(); j --> 0;) {
			add(in(), i, 0, h);
			du[i]++;
		}
	}
    dij();
	printf("%d
", std::max(dis[n], f[n]));
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10265100.html