BZOJ 1179 [Tarjan][spfa]

1179: [Apio2009]Atm
Description
这里写图片描述
Input

第一行包含两个整数N、M。N表示路口的个数,M表示道路条数。接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号。接下来N行,每行一个整数,按顺序表示每个路口处的ATM机中的钱数。接下来一行包含两个整数S、P,S表示市中心的编号,也就是出发的路口。P表示酒吧数目。接下来的一行中有P个整数,表示P个有酒吧的路口的编号
Output
输出一个整数,表示Banditji从市中心开始到某个酒吧结束所能抢劫的最多的现金总数。
Sample Input
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
Sample Output
47
HINT
50%的输入保证N, M<=3000。所有的输入保证N, M<=500000。每个ATM机中可取的钱数为一个非负整数且不超过4000。输入数据保证你可以从市中心沿着Siruseri的单向的道路到达其中的至少一个酒吧。

感觉挺简单的,tarjan一遍,缩点后的图求最长路即可

附代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#define N 500010
using namespace std;

int scccnt = 0, cnt = 0, NewCnt = 0, dfs_clock = 0, ans = 0;
int n, m, s, x, y, p, start;
int sccno[N], pre[N], lowlink[N], head[N];
int NewHead[N], dist[N], node1[N], node2[N];
bool vis[N], isbar[N], newbar[N];
struct edge {
  int to, next;
  edge(void) {}
  edge(int t, int n):to(t), next(n) {}
}G[N], NewG[N];
map<pair<int, int>, bool> Map;
stack<int> S;
queue<int> Q;

inline char get(void) {
  static char buf[1000000], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
    if (p2 == p1) return EOF;
  }
  return *p1++;
}
inline void read(int &x) {
  x = 0; char c;
  for(c = get(); c < '0' || c > '9'; c = get());
  for(; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = get());
}
inline void add_edge(int from, int to) {
  G[cnt] = edge(to, head[from]);
  head[from] = cnt++;
}
inline void add_new_edge(int from, int to) {
  NewG[NewCnt] = edge(to, NewHead[from]);
  NewHead[from] = NewCnt++;
}
void dfs(int x) {
  pre[x] = lowlink[x] = ++dfs_clock; S.push(x);
  for(int i = head[x]; i != -1; i = G[i].next) {
    int v = G[i].to;
    if (!pre[v]) {
      dfs(v); lowlink[x] = min(lowlink[x], lowlink[v]);
    } else if (!sccno[v]){
      lowlink[x] = min(lowlink[x], pre[v]);
    }
  }
  if (lowlink[x] == pre[x]) {
    scccnt++;
    for(;;) {
      int u = S.top(); S.pop();
      sccno[u] = scccnt;
      node2[scccnt] += node1[u];
      if (isbar[u]) newbar[scccnt] = 1;
      if (x == u) break;
    }
  }
}
void tarjan(void) {
  for(int i = 1; i <= n; i++) {
    if (!pre[i]) dfs(i);
  }
}
void spfa(int s) {
  Q.push(s); vis[s] = 1;
  while(!Q.empty()) {
    int x = Q.front(); Q.pop(); vis[x] = 0;
    for(int i = NewHead[x]; i != -1; i = NewG[i].next) {
      int v = NewG[i].to;
      if (node2[v] + dist[x] > dist[v]) {
        dist[v] = node2[v] + dist[x];
        if (!vis[v]) {
          vis[v] = 1; Q.push(v);
        }
      }
    }
  }
}

int main(void) {
  memset(head, -1, sizeof(head));
  memset(NewHead, -1, sizeof(NewHead));
  read(n); read(m);
  for(int i = 0; i < m; i++) {
    read(x); read(y);
    add_edge(x, y);
  }
  for(int i = 1; i <= n; i++) read(node1[i]);
  read(s); read(p);
  for(int i = 0; i < p; i++) {
    read(x); isbar[x] = 1;
  }
  tarjan(); start = sccno[s];
  for(int i = 1; i <= n; i++) {
    for(int j = head[i]; j != -1; j = G[j].next) {
      int a = sccno[i], b = sccno[G[j].to];
      if (a != b) {
        add_new_edge(a, b);
      }
    }
  }
  spfa(start);
  for(int i = 1; i <= scccnt; i++) {
    if (newbar[i]) ans = max(ans, dist[i]);
  }
  cout << ans + node2[start] << endl;
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值