1179: [Apio2009]Atm

Time Limit: 15 Sec  Memory Limit: 162 MB
Submit: 3250  Solved: 1346
[Submit][Status][Discuss]

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的单向的道路到达其中的至少一个酒吧。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;
#define N 500001
stack<int>Sta;
queue<int>Que;
vector<int>Gra[N],newGra[N];
bool isBar[N],inStack[N];
int low[N],dfn[N],belong[N],money[N],newMoney[N],cnt,Time,start;
int maxMoney[N];
void Tarjan(int s)
{
    dfn[s] = low[s] = ++Time;
    Sta.push(s);
    inStack[s] = true;
    for(int i=0;i<Gra[s].size();i++)
    {
        int j = Gra[s][i];
        if(dfn[j] == 0){
            Tarjan(j);
            low[s] = min(low[s], low[j]);
        }
        else if(inStack[j] == true){
            low[s] = min(low[s], dfn[j]);
        }
    }
    if(dfn[s] == low[s])
        {
            cnt ++;
            while(!Sta.empty()){
              int temp = Sta.top(); Sta.pop();
              belong[temp] = cnt;
              newMoney[cnt] += money[temp];
              inStack[temp] = false;
              if(temp == s) break;
            }
        }
}
 
int spfa()
{
    int ans = 0;
    Que.push(start);
    memset(maxMoney,0,sizeof(maxMoney));
    maxMoney[start] = newMoney[start];
    while(!Que.empty())
    {
        int now = Que.front(); Que.pop();
        for(int i = 0; i < newGra[now].size(); i++)
        {
            int j = newGra[now][i];
            if(maxMoney[now] + newMoney[j] > maxMoney[j])
            {
                maxMoney[j] = maxMoney[now] + newMoney[j];
                Que.push(j);
            }
            if(isBar[j]) ans = max(ans, maxMoney[j]);
        }
    }
    return ans;
}
 
int main()
{
    int n,m,a,b,s,p;
    scanf("%d%d",&n,&m);
    for (int i = 0; i < m; i++) {
      /* code */
      scanf("%d%d",&a, &b);
      Gra[a].push_back(b);
    }
    for (int i = 1; i <= n; i++) {
      /* code */
      scanf("%d",&money[i]);
    }
    scanf("%d%d",&s,&p);
    memset(inStack,false,sizeof(inStack));
    memset(newMoney,0,sizeof(newMoney));
    memset(isBar,false,sizeof(isBar));
    memset(dfn,0,sizeof(dfn));
    cnt = Time = 0;
    for(int i = 1; i <= n; i++) if(dfn[i] == 0) Tarjan(i);
    if(cnt == 1) {printf("%d",newMoney[cnt]);return 0;}
    for(int i=0;i<p;i++)
    {
      scanf("%d", &a);
      isBar[belong[a]] = true;
    }
    start = belong[s];
    for(int i =1; i <= n; i ++)
    {
        for(int j = 0; j < Gra[i].size(); j++){
            int k = Gra[i][j];
            if(belong[i] != belong[k]){
                newGra[belong[i]].push_back(belong[k]);
            }
        }
    }
 
    int ans = spfa();
    printf("%d", ans);
}

 

 看起来,STL和链式向前星相比,速度还是慢了不少的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;
#define N 500001
stack<int>Sta;
queue<int>Que;
struct edge
{
    int u,v,next;
}edge1[N],edge2[N];
bool isBar[N],inStack[N];
int low[N],dfn[N],belong[N],money[N],newMoney[N],cnt,Time,start;
int maxMoney[N],head[N],head2[N];
 
void add(int u, int v, int id)
{
    edge1[id].u = u;
    edge1[id].v = v;
    edge1[id].next = head[u];
    head[u] = id;
}
 
void add2(int u, int v, int id)
{
    edge2[id].u = u;
    edge2[id].v = v;
    edge2[id].next = head2[u];
    head2[u] = id;
}
void Tarjan(int s)
{
    dfn[s] = low[s] = ++Time;
    Sta.push(s);
    inStack[s] = true;
    for(int u = head[s]; ~u; u = edge1[u].next)
    {
        int v = edge1[u].v;
        if(dfn[v] == 0){
            Tarjan(v);
            low[s] = min(low[s], low[v]);
        }
        else if(inStack[v] == true){
            low[s] = min(low[s], dfn[v]);
        }
    }
    if(dfn[s] == low[s])
        {
            cnt ++;
            while(!Sta.empty()){
              int temp = Sta.top(); Sta.pop();
              belong[temp] = cnt;
              newMoney[cnt] += money[temp];
              inStack[temp] = false;
              if(temp == s) break;
            }
        }
}
 
int spfa()
{
    int ans = 0;
    Que.push(start);
    memset(maxMoney,0,sizeof(maxMoney));
    maxMoney[start] = newMoney[start];
    while(!Que.empty())
    {
        int now = Que.front(); Que.pop();
        for(int u = head2[now]; ~u; u = edge2[u].next)
        {
            int v = edge2[u].v;
            if(maxMoney[now] + newMoney[v] > maxMoney[v])
            {
                maxMoney[v] = maxMoney[now] + newMoney[v];
                Que.push(v);
            }
            if(isBar[v]) ans = max(ans, maxMoney[v]);
        }
    }
    return ans;
}
 
int main()
{
    int n,m,a,b,s,p;
    scanf("%d%d",&n,&m);
    memset(head,-1,sizeof(head));
    for (int i = 0; i < m; i++) {
      /* code */
      scanf("%d%d",&a, &b);
      add(a,b,i+1);
    }
    for (int i = 1; i <= n; i++) {
      /* code */
      scanf("%d",&money[i]);
    }
    scanf("%d%d",&s,&p);
    memset(inStack,false,sizeof(inStack));
    memset(newMoney,0,sizeof(newMoney));
    memset(isBar,false,sizeof(isBar));
    memset(dfn,0,sizeof(dfn));
    cnt = Time = 0;
    for(int i = 1; i <= n; i++) if(dfn[i] == 0) Tarjan(i);
    if(cnt == 1) {printf("%d",newMoney[cnt]);return 0;}
    for(int i=0;i<p;i++)
    {
      scanf("%d", &a);
      isBar[belong[a]] = true;
    }
    start = belong[s];
    memset(head2,-1,sizeof(head2));
    int kkk = 0;
    for(int i =1; i <= n; i ++)
    {
        for(int u = head[i]; ~u; u = edge1[u].next){
            int v = edge1[u].v;
            if(belong[i] != belong[v]){
                add2(belong[i],belong[v],++kkk);
            }
        }
    }
 
    int ans = spfa();
    printf("%d", ans);
}

Tarjan + bfs HYSBZ 1179Atm的更多相关文章

  1. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  2. NOIP 2015提高组复赛

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...

  3. hdu-4612(无向图缩点+树的直径)

    题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...

  4. Codeforces Round #467(Div2)题解

    凌晨起来打CF,0:05,也是我第一次codeforces 第一题: 我刚开始怀疑自己读错题了,怎么会辣么水. 判除了0的数字种类 #include <cstdio> ; ]; int m ...

  5. 【题解】洛谷P3119 Grass Cownoisseur G

    题面:洛谷P3119 Grass Cownoisseur G 本人最近在熟悉Tarjan的题,刷了几道蓝题后,我飘了 趾高气扬地点开这道紫题,我一瞅: 哎呦!这不是分层图吗? 突然就更飘了~~~ 用时 ...

  6. 【BZOJ-2725】故乡的梦 Dijsktra + Tarjan + Dinic + BFS + 堆

    2725: [Violet 6]故乡的梦 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 502  Solved: 173[Submit][Status ...

  7. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  8. BZOJ 1194: [HNOI2006]潘多拉的盒子( BFS + tarjan + dp )

    O(S²)枚举2个诅咒机, 然后O(n²)BFS去判断. 构成一个有向图, tarjan缩点, 然后就是求DAG的最长路.. ------------------------------------- ...

  9. 【BFS】【并查集】【Tarjan】【LCA】Gym - 101173H - Hangar Hurdles

    给你一张地图,给你q次询问,每次问你从A点到B点,最大能移动多大的箱子. 把每个点所能容纳的最大箱子求出来(BFS,八连通,一开始将所有边界点和障碍点入队).然后从大到小排序.然后用并查集将相邻(四联 ...

随机推荐

  1. Vb.net MakeLong MAKELPARAM 合并整数代码

    Function MAKELPARAM(wLow As UShort, wHigh As UShort) As UInteger Return wHigh * &H10000 + wLow E ...

  2. headfirst设计模式(6)—单例模式

    前言 这一章的课题看起来就很和蔼可亲了,比起前面绕的我不要不要的工厂模式,那感觉真是太好了,但是正是因为简单,那么问题就来了,我怎么才能把这个东西叙述清楚?怎么样才能老少咸宜呢? 如何能够在把这个东西 ...

  3. Vue开发插件

    (一)Vue.js的插件应该有一个公开方法:install. 这个方法的第一个参数是Vue构造器,第二个参数是一个可选的选项对象,一般是如下操作: MyPlugin.install = functio ...

  4. Salesforce的对象和字段

    对象 Salesforce默认提供了很多功能,可以用于销售.市场开发.客服等.为了实现这些功能,Salesforce提供了一系列的标准对象,比如"客户"(Account).&quo ...

  5. Python绘图与可视化

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  6. <自动化测试方案_9>第九章、持续集成平台搭建

    第九章.持续集成平台搭建 (一)什么是持续集成 参考文章地址:https://blog.csdn.net/qq_32261399/article/details/76651376 敏捷软件开发(英语: ...

  7. 【视频】设计模式(Java)视频讲解

    设计模式(JAVA) 视频网址: http://www.qghkt.com/ 设计模式(JAVA)视频地址: https://ke.qq.com/course/318643?tuin=a508ea62 ...

  8. 自研数据库CynosDB存储系统如何实现即时恢复

    本文由云+社区发表 本文作者:许中清,腾讯云自研数据库CynosDB的分布式存储CynosStore负责人.从事数据库内核开发.数据库产品架构和规划.曾就职于华为,2015年加入腾讯,参与过TBase ...

  9. android申请多个权限的正确姿势

    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permi ...

  10. SpringBoot中集成Swagger2

    1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...