【Apio2009】Bzoj1179 Atm
Position:
List
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的单向的道路到达其中的至少一个酒吧。
Solution
先Tarjian缩点,变成一个DAG(有向无环图)不会Tarjan Algorithm的传送门
接下来就好做了:
- Dfs直接上,虽然Bzoj不会卡你,可今天考试丧心病狂的数据把我卡成了90
- 记忆化搜索,f[x]记录从x走到尾的最大抢钱数,下次再走到这点就可以直接调用,还是比dfs快蛮多。
- 也可以跑最短路(Spfa or Dijstra)
Code
Dfs
// <atm.cpp> - Tue Sep 20 08:15:49 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=100010;
const int MAXM=100010;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
struct Tarjan{
static const int N=500010,M=500010;
int n,m,be,tot,_clock,scc;
int ne[M],to[M];bool in[N];vector<int>p[N];
int bar[N],fr[N],low[N],dfn[N],f[N],st[N],s[N],ss[N];
void add(int u,int v){
to[++tot]=v;ne[tot]=fr[u];fr[u]=tot;
}
void Init(){
n=gi(),m=gi();
for(int i=1;i<=m;i++){
int u=gi(),v=gi();add(u,v);
}
for(int i=1;i<=n;i++)ss[i]=gi();
be=gi(),m=gi();
for(int i=1;i<=m;i++)bar[i]=gi();
}
inline void dfs(int x){
dfn[x]=low[x]=++_clock;
st[++tot]=x;in[x]=true;
for(int o=fr[x];o;o=ne[o])
if(!dfn[to[o]])dfs(to[o]),low[x]=min(low[x],low[to[o]]);
else if(in[to[o]])low[x]=min(low[x],low[to[o]]);
if(dfn[x]==low[x]){
scc++;
while(st[tot]!=x){
f[st[tot]]=scc;in[st[tot]]=false;tot--;
}
f[x]=scc;in[x]=false;tot--;
}
}
void src(int x,int now){
if(now<=f[x])return;f[x]=now;
int to=p[x].size();
for(int i=0;i<to;i++)
src(p[x][i],now+s[p[x][i]]);
}
void Work(){
_clock=tot=scc=0;
memset(dfn,0,sizeof(dfn));
memset(in,false,sizeof(in));
for(int i=1;i<=n;i++)
if(!dfn[i])dfs(i);
for(int i=1;i<=n;i++)s[f[i]]+=ss[i];
for(int i=1;i<=n;i++)
for(int o=fr[i];o;o=ne[o]){
if(f[i]==f[to[o]])continue;
p[f[i]].push_back(f[to[o]]);
}
for(int i=1;i<=m;i++)bar[i]=f[bar[i]];
be=f[be];memset(f,0,sizeof(f));
src(be,s[be]);int ans=0;
for(int i=1;i<=m;i++)ans=max(ans,f[bar[i]]);
printf("%d",ans);
}
}Tar;
int main()
{
freopen("atm.in","r",stdin);
freopen("atm.out","w",stdout);
Tar.Init();Tar.Work();
return 0;
}
记忆化搜索
// <atm.cpp> - Tue Sep 20 08:15:49 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=100010;
const int MAXM=100010;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
struct Tarjan{
static const int N=500010,M=500010;
int n,m,be,tot,_clock,scc;
int ne[M],to[M];bool in[N];vector<int>p[N];
int bar[N],fr[N],low[N],dfn[N],f[N],st[N],s[N],ss[N];
void add(int u,int v){
to[++tot]=v;ne[tot]=fr[u];fr[u]=tot;
}
void Init(){
n=gi(),m=gi();
for(int i=1;i<=m;i++){
int u=gi(),v=gi();add(u,v);
}
for(int i=1;i<=n;i++)ss[i]=gi();
be=gi(),m=gi();
for(int i=1;i<=m;i++)bar[i]=gi();
}
inline void dfs(int x){
dfn[x]=low[x]=++_clock;
st[++tot]=x;in[x]=true;
for(int o=fr[x];o;o=ne[o])
if(!dfn[to[o]])dfs(to[o]),low[x]=min(low[x],low[to[o]]);
else if(in[to[o]])low[x]=min(low[x],low[to[o]]);
if(dfn[x]==low[x]){
scc++;
while(st[tot]!=x){
f[st[tot]]=scc;in[st[tot]]=false;tot--;
}
f[x]=scc;in[x]=false;tot--;
}
}
inline int src(int x){//let every node just go once
if(in[x])return f[x];in[x]=true;
int to=p[x].size();
for(int i=0;i<to;i++)
f[x]=max(f[x],src(p[x][i]));
if(f[x]||st[x])f[x]+=s[x];
return f[x];
}
void Work(){
_clock=tot=scc=0;
memset(dfn,0,sizeof(dfn));
memset(in,false,sizeof(in));
for(int i=1;i<=n;i++)
if(!dfn[i])dfs(i);
for(int i=1;i<=n;i++)s[f[i]]+=ss[i];
for(int i=1;i<=n;i++)
for(int o=fr[i];o;o=ne[o]){
if(f[i]==f[to[o]])continue;
p[f[i]].push_back(f[to[o]]);
}
memset(st,0,sizeof(st));memset(in,0,sizeof(0));
for(int i=1;i<=m;i++)bar[i]=f[bar[i]],st[bar[i]]=1;
be=f[be];memset(f,0,sizeof(f));
printf("%d",src(be));
}
}Tar;
int main()
{
freopen("atm.in","r",stdin);
freopen("atm.out","w",stdout);
Tar.Init();Tar.Work();
return 0;
}
【Apio2009】Bzoj1179 Atm的更多相关文章
- 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm
缩点后转化成 DAG图上的单源最长路问题.spfa/dp随便. #include<cstdio> #include<queue> #include<algorithm&g ...
- 【BZOJ】【1177】【APIO2009】Oil
DP 找出三个正方形,可以转化为将整个油田切成三个矩形块,每块中各找一个正方形区域,切的形式只有6种,分类更新ans即可 题解:http://trinklee.blog.163.com/blog/st ...
- 【BZOJ】【1178】【APIO2009】convention会议中心
贪心 如果不考虑字典序的话,直接按右端点排序,能选就选,就可以算出ans…… 但是要算一个字典序最小的解就比较蛋疼了= = Orz了zyf的题解 就是按字典序从小到大依次枚举,在不改变答案的情况下,能 ...
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- 【BZOJ4317】Atm的树 动态树分治+二分+线段树
[BZOJ4317]Atm的树 Description Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径 ...
- 【题解】[APIO2009]会议中心
[题解][P3626 APIO2009]会议中心 真的是一道好题!!!刷新了我对倍增浅显的认识. 此题若没有第二份输出一个字典序的方案,就是一道\(sort+\)贪心,但是第二问使得我们要用另外的办法 ...
- 【动态规划】HDU 5781 ATM Mechine
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 题目大意: 一个人有[0,K]内随机的钱,每次可以随意取,但是不知道什么时候取完,取钱超过剩余 ...
- 【tarjan+SPFA】BZOJ1179-[Apio2009]Atm
[题目大意] 给出一张有点权的有向图,已知起点和可以作为终点的一些点,问由起点出发,每条边和每个点可以经过任意多次,经过点的权值总和最大为多少. [思路] 由于可以走任意多次,显然强连通分量可以缩点. ...
- 【NOI2014】起床困难综合症(贪心)
[NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...
随机推荐
- java基础学习之垃圾回收机制
回收过程: 1.发现无用的对象 2.回收无用对象占用的内存的空间. 垃圾回收相关算法: 1.引用计数法 堆中每个对象都有一个引用计数.被引用一次,计数加一.被引用变量值变为null,则计数减一. 到计 ...
- freemarker使用map替换ftl中相关值
ftl文件demo01.ftl <html> <head> <title>Welcome!</title> </head> <body ...
- idea搭建maven项目 【转发】
为了创建maven项目可是花了我时间了,网上的教程跟我的实际情况不符合,尤其是facets .artifacts 那块.幸亏找到这篇文章没解决了我的问题,他的描述跟我的情况一模一样.这篇文章竟然来自百 ...
- zabbix3.4调用钉钉报警通知(超详细)
一.备注: zabbix调用钉钉接口报警通知有两种情况: 1.通知到个人钉 2.通知到钉钉群 本文主要介绍zabbix调用钉钉接口通知到钉钉个人的方式 二.zabbix3.4调用钉钉接口报警通知到个 ...
- java 十五周总结
- 10 Python中的代码缓存机制
目录: 1) 什么是代码块 2) 基本原理 3) 机制适用范围 4) 适用对象 5) 优势 更详细说明,参考太白老师博客 https://www.cnblogs.com/jin-xin/article ...
- 04 Python基础数据类型
目录: 1) 整型 2) 为什么使用16进制以及用在哪里 3) 浮点型 4) 字符串型 5) 布尔型 6) 查看数据类型 7) 复数型 8) input() 9) print() 10) % 格式化输 ...
- 【Codeforces 364A】Matrix
[链接] 我是链接,点我呀:) [题意] 让你求出b[i][j]=s[i]*s[j]规则构成的矩阵 的所有子矩阵中子矩阵的和为a的子矩阵的个数 [题解] (x,y,z,t) 会发现它的和就是sum(x ...
- python整数转ASCII码
# *-* coding:utf-8 *-* import binascii data = [1441465642, 251096121, -870437532, -944322827, 647240 ...
- Uva122 Trees on the level
Background Trees are fundamental in many branches of computer science. Current state-of-the art para ...