bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)
3876: [Ahoi2014]支线剧情
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 484 Solved: 296
[Submit][Status][Discuss]
Description
着游戏的进行,剧情是不可逆的。所以游戏保证从任意剧情点出发,都不能再回到这个剧情点。由于JYY过度使用修改器,导致游戏的“存档”和“读档”功能损
坏了,
Input
Output
输出一行包含一个整数,表示JYY看完所有支线剧情所需要的最少时间。
Sample Input
2 2 1 3 2
2 4 3 5 4
2 5 5 6 6
0
0
0
Sample Output
HINT
JYY需要重新开始3次游戏,加上一开始的一次游戏,4次游戏的进程是
Source
【思路】
有上下界的费用流。
用最短路算法慢得飞起=-=,等会学一下zkw费用流。
【代码】
- /**************************************************************
- Problem: 3876
- User: hahalidaxin2
- Language: C++
- Result: Accepted
- Time:10640 ms
- Memory:1856 kb
- ****************************************************************/
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<vector>
- #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
- using namespace std;
- typedef long long LL ;
- const int maxn = +;
- const int INF = 1e9;
- struct Edge{ int u,v,cap,flow,cost;
- };
- struct MCMF {
- int n,m,s,t;
- int inq[maxn],a[maxn],d[maxn],p[maxn];
- vector<int> G[maxn];
- vector<Edge> es;
- void init(int n) {
- this->n=n;
- es.clear();
- for(int i=;i<n;i++) G[i].clear();
- }
- void AddEdge(int u,int v,int cap,int cost) {
- es.push_back((Edge){u,v,cap,,cost});
- es.push_back((Edge){v,u,,,-cost});
- m=es.size();
- G[u].push_back(m-);
- G[v].push_back(m-);
- }
- bool SPFA(int s,int t,int& flow,LL& cost) {
- for(int i=;i<n;i++) d[i]=INF;
- memset(inq,,sizeof(inq));
- d[s]=; inq[s]=; p[s]=; a[s]=INF;
- queue<int> q; q.push(s);
- while(!q.empty()) {
- int u=q.front(); q.pop(); inq[u]=;
- for(int i=;i<G[u].size();i++) {
- Edge& e=es[G[u][i]];
- int v=e.v;
- if(e.cap>e.flow && d[v]>d[u]+e.cost) {
- d[v]=d[u]+e.cost;
- p[v]=G[u][i];
- a[v]=min(a[u],e.cap-e.flow); //min(a[u],..)
- if(!inq[v]) { inq[v]=; q.push(v);
- }
- }
- }
- }
- if(d[t]==INF) return false;
- flow+=a[t] , cost += (LL) a[t]*d[t];
- for(int x=t; x!=s; x=es[p[x]].u) {
- es[p[x]].flow+=a[t]; es[p[x]^].flow-=a[t];
- }
- return true;
- }
- int Mincost(int s,int t,LL& cost) {
- int flow=; cost=;
- while(SPFA(s,t,flow,cost)) ;
- return flow;
- }
- } mc;
- int n;
- int main() {
- scanf("%d",&n);
- mc.init(n+);
- int s=,t=n+;
- mc.AddEdge(t,s,INF,);
- FOR(u,,n) {
- int m,v,c; scanf("%d",&m);
- if(u!=) mc.AddEdge(u,,INF,);
- mc.AddEdge(u,t,m,);
- FOR(j,,m) {
- scanf("%d%d",&v,&c);
- mc.AddEdge(u,v,INF,c);
- mc.AddEdge(s,v,,c);
- }
- }
- LL cost;
- mc.Mincost(s,t,cost);
- printf("%lld\n",cost);
- return ;
- }
最短路算法
【代码2】
- /**************************************************************
- Problem: 3876
- User: hahalidaxin2
- Language: C++
- Result: Accepted
- Time:1884 ms
- Memory:1852 kb
- ****************************************************************/
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<vector>
- #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
- using namespace std;
- typedef long long LL ;
- const int maxn = +;
- const int INF = 1e9;
- struct Edge{ int u,v,cap,flow,cost;
- };
- struct zkw {
- int n,m,s,t;
- int vis[maxn],d[maxn];
- vector<int> G[maxn];
- vector<Edge> es;
- void init(int n) {
- this->n=n;
- es.clear();
- for(int i=;i<n;i++) G[i].clear();
- }
- void AddEdge(int u,int v,int cap,int cost) {
- es.push_back((Edge){u,v,cap,,cost});
- es.push_back((Edge){v,u,,,-cost});
- m=es.size();
- G[u].push_back(m-);
- G[v].push_back(m-);
- }
- bool spfa() {
- memset(vis,,sizeof(vis));
- for(int i=;i<n;i++) d[i]=INF;
- queue<int> q;
- d[t]= , vis[t]= , q.push(t);
- while(!q.empty()) {
- int u=q.front(); q.pop() , vis[u]=;
- for(int i=;i<G[u].size();i++) {
- Edge& e=es[G[u][i]];
- int v=e.v;
- if(es[G[u][i]^].cap && d[v]>d[u]-e.cost) {
- d[v]=d[u]-e.cost;
- if(!vis[v]) {
- vis[v]=;
- q.push(v);
- }
- }
- }
- }
- return d[s]!=INF;
- }
- int dfs(int u,int a,LL& cost) {
- vis[u]=; if(u==t) return a;
- int used=,w;
- for(int i=;i<G[u].size();i++) {
- Edge& e=es[G[u][i]];
- int v=e.v;
- if(d[u]-e.cost==d[v] && !vis[v] && e.cap) {
- w=dfs(v,min(a-used,e.cap),cost);
- cost+=w*e.cost;
- e.cap-=w , es[G[u][i]^].cap+=w;
- used+=w; if(used==a) return a;
- }
- }
- return used;
- }
- int Mincost(int s,int t,LL& cost) {
- this->s=s , this->t=t;
- int flow=; cost=;
- while(spfa()) {
- vis[t]=;
- while(vis[t]) {
- memset(vis,,sizeof(vis));
- flow+=dfs(s,INF,cost);
- }
- }
- return flow;
- }
- } mc;
- int n;
- int main() {
- scanf("%d",&n);
- mc.init(n+);
- int s=,t=n+;
- mc.AddEdge(t,s,INF,);
- FOR(u,,n) {
- int m,v,c; scanf("%d",&m);
- if(u!=) mc.AddEdge(u,,INF,);
- mc.AddEdge(u,t,m,);
- FOR(j,,m) {
- scanf("%d%d",&v,&c);
- mc.AddEdge(u,v,INF,c);
- mc.AddEdge(s,v,,c);
- }
- }
- LL cost;
- mc.Mincost(s,t,cost);
- printf("%lld\n",cost);
- return ;
- }
zkw费用流
跑稠密图的话,zkw的优化效果还是比较明显的。
UPD.16/4/8
按理说有上下界的处理和跑网络流的那几道题是一样的,现在没时间测了,该上车了。
bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)的更多相关文章
- BZOJ 3876: [Ahoi2014]支线剧情 [上下界费用流]
3876: [Ahoi2014]支线剧情 题意:每次只能从1开始,每条边至少经过一次,有边权,求最小花费 裸上下界费用流...每条边下界为1就行了 注意要加上下界*边权 #include <io ...
- BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流
3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...
- BZOJ 3876:支线剧情(有下界最小费用最大流)
3876: [Ahoi2014]支线剧情 Description [故事背景]宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧 ...
- 【BZOJ3876】[Ahoi2014]支线剧情 有上下界费用流
[BZOJ3876][Ahoi2014]支线剧情 Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩 ...
- 【有源汇上下界费用流】BZOJ 3876 [Ahoi2014]支线剧情
题目链接: http://www.lydsy.com:808/JudgeOnline/problem.php?id=3876 题目大意: 给定一张拓扑图(有向无环图),每条边有边权,每次只能从第一个点 ...
- bzoj 3876: [Ahoi2014]支线剧情
就是加一个1的下界就好了. #include<bits/stdc++.h> #define N 100005 #define LL long long #define inf 0x3f3f ...
- 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 502[Submit][Status ...
- BZOJ3876[Ahoi2014&Jsoi2014]支线剧情——有上下界的最小费用最大流
题目描述 [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现在JYY想花费 ...
- [AHOI2014/JSOI2014]支线剧情 有上下界费用流
---题面--- 题解: 第一眼费用流,,然后想了好久怎么建图,,,最后发现是最小费用可行流的板子题.... 其实还没有很懂这个算法,所以这里只是摆一下步骤,以后再补理解吧. 首先一个思路就是转换图, ...
随机推荐
- 内网映射到公网工具 --- ngrok
ngrok可以将内网映射到公网上,这样就可以在公网上访问你的网络服务. 该工具通常在进行app开发和微信开发时比较有用,这样就可避免在公网服务器上单独部署项目,通过映射,直接连接本地服务即可进行开发. ...
- SGU 197.Nice Patterns Strike Back
时间限制:0.5s 空间限制:6M 题意: 给出长n(n<=10^100)和宽m(m<=5)的地面,铺上黑色和白色的地板,使得没有任意一个2*2大小的地面铺同种颜色的方案数是多少. Sol ...
- Python学习 - 使用BeautifulSoup来解析网页一:基础入门
写技术博客主要就是总结和交流的,如果文章用错,请指正啊! 以前一直在使用SGMLParser,这个太费时间和精力了,现在为了毕业设计,改用BeautifulSoup来实现HTML页面的解析工作的. 一 ...
- jquery获取元素的所有宽高(包括内边距和外边距)
width() - 返回元素的宽度.height() - 返回元素的高度.innerWidth() 方法返回元素的宽度(包括内边距). innerHeight() ...
- 安装search everything中文语言包
Everything 作为很多人的必备工具,特写这篇文章,一方面让想使用国外优秀软件的英语小白有一段过渡期,另一方面也为自己整理下资料.当然,这个可不是不学好英语的正当理由. 步骤: 1. 下载好se ...
- frame,bounds,center-三者的含义
frame与bounds的区别比较 frame,bounds,center-三者的含义 偶然觉的,这三个属性有时候定位的时候,需要用.于是就来搞清楚,到底frame,bounds,center 这三个 ...
- List of XML and HTML character entity references
A character entity reference refers to the content of a named entity. An entity declaration is creat ...
- BZOJ 3570 动物园
Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...
- 源代码安装GIT
参考URL:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25150840&id=4250659 若是条件允许,从源代 ...
- JavaScript encodeURI() 函数
encodeURI() 函数可把字符串作为 URI 进行编码. -------------------------------------------------------------------- ...