【BZOJ1179】[Apio2009]Atm【SCC】【最长路】

【题目链接】

tarjan先缩点,然后跑个最长路。读入一个酒吧位置就更新一次答案。

/* Pigonometry */
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> pii;

const int maxn = 500005, maxs = maxn;

int n, m, head[maxn], cnt, dfn[maxn], low[maxn], clo, belong[maxn], tot, dp[maxn], w[maxn];
bool vis[maxn];
vector<int> mp[maxn];
priority_queue<pii, vector<pii>, less<pii> > q;

struct _edge {
	int v, next;
} g[maxn << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

int top, sta[maxn];

inline void tarjan(int x) {
	dfn[x] = low[x] = ++clo;
	vis[sta[++top] = x] = 1;
	for(int i = head[x]; ~i; i = g[i].next) {
		int v = g[i].v;
		if(!dfn[v]) tarjan(v), low[x] = min(low[x], low[v]);
		else if(vis[v]) low[x] = min(low[x], dfn[v]);
	}
	if(dfn[x] == low[x]) {
		tot++;
		while(1) {
			int u = sta[top--];
			belong[u] = tot;	
			vis[u] = 0;
			if(u == x) break;
		}
	}
}

inline void dijkstra(int S) {
	dp[S] = w[S];
	q.push(pii(dp[S], S));
	while(!q.empty()) {
		pii u = q.top(); q.pop();
		int x = u.second;
		if(dp[x] > u.first) continue;
		for(int i = 0; i < mp[x].size(); i++) {
			int v = mp[x][i];
			if(dp[v] < dp[x] + w[v]) {
				dp[v] = dp[x] + w[v];
				q.push(pii(dp[v], v));
			}
		}
	}
}	

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = top = 0;

	for(int i = 1; i <= m; i++) {
		int u = iread(), v = iread();
		add(u, v);
	}

	for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i);
	for(int i = 1; i <= n; i++) w[belong[i]] += iread();

	for(int i = 1; i <= n; i++) for(int j = head[i]; ~j; j = g[j].next) if(belong[i] ^ belong[g[j].v])
		mp[belong[i]].push_back(belong[g[j].v]);

	int S = iread(), p = iread(), ans = 0;
	dijkstra(belong[S]);
	for(int i = 1; i <= p; i++) {
		int u = iread();
		ans = max(ans, dp[belong[u]]);
	}

	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(强连通,最长路)