【bzoj1179】[Apio2009]Atm
*题目描述:
*输入:
第一行包含两个整数N、M。N表示路口的个数,M表示道路条数。接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号。接下来N行,每行一个整数,按顺序表示每个路口处的ATM机中的钱数。接下来一行包含两个整数S、P,S表示市中心的编号,也就是出发的路口。P表示酒吧数目。接下来的一行中有P个整数,表示P个有酒吧的路口的编号
*输出:
输出一个整数,表示Banditji从市中心开始到某个酒吧结束所能抢劫的最多的现金总数。
*样例输入:
6 7
1 2
2 3
3 5
2 4
4 1
2 6
6 5
10
12
8
16
1
5
1 4
4
3
5
6
样例输出:
47
*提示:
50%的输入保证N, M<=3000。所有的输入保证N, M<=500000。每个ATM机中可取的钱数为一个非负整数且不超过4000。输入数据保证你可以从市中心沿着Siruseri的单向的道路到达其中的至少一个酒吧。
*题解:
缩强+最短路。把强连通分量都缩起来,然后构造新图,求出新图点权的最长路即可。
*代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif
#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 500010
#define maxm 1000010
struct Edge
{
Edge *next;
int to;
}*last[maxn], e[maxm], *ecnt = e, *last2[maxn];
inline void link(R int a, R int b)
{
*++ecnt = (Edge) {last[b], a}; last[b] = ecnt;
}
inline void link2(R int a, R int b)
{
*++ecnt = (Edge) {last2[a], b}; last2[a] = ecnt;
}
int val[maxn], dfn[maxn], low[maxn], timer, st[maxn], stcnt, scc, id[maxn], sum[maxn];
void dfs(R int x)
{
dfn[x] = low[x] = ++timer;
st[++stcnt] = x;
for (R Edge *iter = last[x]; iter; iter = iter -> next)
{
R int pre = iter -> to;
if (!dfn[pre])
{
dfs(pre);
cmin(low[x], low[pre]);
}
else if (!id[pre]) cmin(low[x], dfn[pre]);
}
if (dfn[x] == low[x])
{
++scc;
for ( ; ; )
{
R int now = st[stcnt--];
id[now] = scc;
sum[scc] += val[now];
if (now == x) break;
}
}
}
int d[maxn];
#define inf 0x7fffffff
std::queue <int> q;
bool vis[maxn];
int main()
{
// setfile();
R int n = FastIn(), m = FastIn();
for (R int i = 1; i <= m; ++i)
link(FastIn(), FastIn());
for (R int i = 1; i <= n; ++i) val[i] = FastIn();
for (R int i = 1; i <= n; ++i) if (!dfn[i]) dfs(i);
for (R int i = 1; i <= n; ++i)
{
for (R Edge *iter = last[i]; iter; iter = iter -> next)
if (id[i] != id[iter -> to]) link2(id[i], id[iter -> to]);
}
for (R int i = 1; i <= scc; ++i) d[i] = -inf;
R int s = id[FastIn()], p = FastIn(), t = scc + 1;
for (R int i = 1; i <= p; ++i) link2(id[FastIn()], t);
d[s] = sum[s]; q.push(s);
while (!q.empty())
{
R int now = q.front(); q.pop();
for (R Edge *iter = last2[now]; iter; iter = iter -> next)
{
R int pre = iter -> to;
if (d[pre] < d[now] + sum[pre])
{
d[pre] = d[now] + sum[pre];
if (!vis[pre])
{
vis[pre] = 1;
q.push(pre);
}
}
}
vis[now] = 0;
}
printf("%d\n", d[t] );
return 0;
}