1. 程式人生 > >CountHunter 6101 最優貿易 強聯通縮點

CountHunter 6101 最優貿易 強聯通縮點

題目傳送門

題解:強連通鎖點之後。 就成了一副單向圖。 然後對於每個點 找到 後面合法的點的最大值就好了。 合法就是後面的那個點可以走到n號點。

程式碼:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define
pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f
; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; const int M = 1e6 + 100; int head[N], to[M], nt[M], tot = 0; int a[N]; vector<int> vc[N]; void add(int u, int v){ to[tot] = v; nt[tot] = head[u]; head[u] = tot++; } int n, m, u, v, op; int belong[N], dfn[N], low[N], now_time, scc_cnt;
int mx[N], mn[N], ret[N]; stack<int> s; void dfs(int u){ dfn[u] = low[u] = ++now_time; s.push(u); for(int i = head[u]; ~i; i = nt[i]){ if(!dfn[to[i]]) dfs(to[i]); if(!belong[to[i]]) low[u] = min(low[u], low[to[i]]); } if(dfn[u] == low[u]){ ++scc_cnt; int now; while(1){ now = s.top(); s.pop(); belong[now] = scc_cnt; if(now == u) break; } } } void scc(int n){ now_time = scc_cnt = 0; for(int i = 1; i <= n; ++i) if(!belong[i]) dfs(i); int v; for(int i = 1; i <= n; ++i){ for(int j = head[i]; ~j; j=nt[j]){ v = to[j]; if(belong[v] != belong[i]){ vc[belong[i]].pb(belong[v]); } } } for(int i = 1; i <= n; ++i){ int rt = belong[i]; if(ret[rt] == 0){ ret[rt] = -1; mn[rt] = a[i]; mx[rt] = a[i]; } else { mn[rt] = min(mn[rt], a[i]); mx[rt] = max(mx[rt], a[i]); } } } int ans = 0; int solve(int x){ if(ret[x] != -1) return mx[x]; ret[x] = 0; int mmax = -1; if(x == belong[n]) mmax = mx[x]; for(int v : vc[x]){ mmax = max(solve(v), mmax); } if(mmax != -1) { mx[x] = max(mx[x], mmax); ans = max(ans, mx[x]-mn[x]); } else mx[x] = -1; return mx[x]; } int main(){ memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); for(int i = 1; i <= m; ++i){ scanf("%d%d%d", &u, &v, &op); add(u, v); if(op == 2) add(v, u); } scc(n); solve(belong[1]); cout << ans << endl; return 0; }
View Code