# poj 3160 Father Christmas flymouse

！！！点权有负值

// poj 3160 Father Christmas flymouse
// wa wa wa ac 1364K 125MS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;

#define MIN(a,b) (a < b ? a : b)
#define clr(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for((i) = (a); (i) < (b); ++i)
#define FORE(i,a,b) for((i) = (a); (i) <= (b); ++i)
#define MAXN 30010
#define MAXM 300010

const int INF = 0x7f7f7f7f;

bool in_stack[MAXN];
bool vis[MAXN];
int num, cnt, scnt, end_sum, m, n, cnt_num;
int in[MAXN];
int cnt0[MAXN];
int dist[MAXN];
int endd[MAXN], val[MAXN];
int low[MAXN], step[MAXN];
int belong[MAXN];
stack<int> st;
struct edge {
int v, nxt;
}e[MAXM];
struct sedge {
int v, nxt, w;
}p[MAXM];

void addedge(int u, int v) {
e[cnt].v = v;
}

void addsedge(int u, int v) {
p[scnt].v = v;
p[scnt].w = endd[v];
}

void Tarjan(int u) {
int v;
int i,j;

step[u] = low[u] = ++num;
st.push(u);
in_stack[u] = true;
for(i = head[u]; i != -1; i = e[i].nxt) {
v = e[i].v;
if(!step[v]) {
Tarjan(v);
low[u] = MIN(low[u],low[v]);
}
else if(in_stack[v])
low[u] = MIN(low[u],step[v]);
}
if(step[u] == low[u]) {
cnt_num++;
int tmp_sum = 0;
do{
j = st.top();
st.pop();
in_stack[j] = false;
if(val[j] > 0)
endd[cnt_num] += val[j];
belong[j] = cnt_num;
}while(j != u);
}
}

int spfa(int x) {
int i;

clr(dist, 0);
clr(cnt0, 0);
clr(vis, false);
dist[x] = endd[x];
queue<int> q;
q.push(x);
vis[x] = true;
++cnt0[x];
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(i = shead[u]; i != -1; i = p[i].nxt){
int v = p[i].v;
if(p[i].w + dist[u] > dist[v]){
dist[v] = p[i].w + dist[u];
if(!vis[v]){
q.push(v);
vis[v] = true;
if((++cnt0[v]) >= cnt_num)
return -1;
}
}
}
}
return 1;
}

void solve() {
int i, j;

FOR(i, 0, n)
if(step[i] == 0)
Tarjan(i);
FOR(i, 0, n)
for(j = head[i]; j != -1; j = e[j].nxt)
if(belong[i] != belong[e[j].v]) {
++in[belong[e[j].v]];
}
FORE(i, 1, cnt_num)
if(in[i] == 0) {
spfa(i);
int tmp_sum = -INF;
FORE(j, 1, cnt_num)
tmp_sum = max(tmp_sum, dist[j]);
end_sum = max(end_sum, tmp_sum);
}
printf("%d\n", end_sum);
}

void init() {
int i, u, v;

clr(step, 0);
clr(in, 0);
clr(belong, 0);
clr(endd, 0);
clr(in_stack, false);
num = cnt = scnt = end_sum = cnt_num = 0;
FOR(i, 0, n)
scanf("%d", &val[i]);
FOR(i, 0, m) {
scanf("%d %d", &u, &v);
}
}

int main() {

while(scanf("%d %d", &n, &m) != EOF) {
init();
solve();
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：poj 3160 Father Christmas flymouse 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)