bzoj 3876(有上下界的最小费用流)
来源:互联网 发布:mac 命令行 删除软件 编辑:程序博客网 时间:2024/05/16 00:53
3876: [Ahoi2014]支线剧情
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 736 Solved: 440
[Submit][Status][Discuss]
Description
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次游戏的进程是
对于100%的数据满足N<=300,0<=Ki<=50,1<=Tij<=300,Sigma(Ki)<=5000
解题思路:莫名其妙的A了。
有上下界的最小费用流:相同构图,然后费用流,不过要加上原来必要边的费用。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF=0x7fffffff;
int n,len,S,SS,TT,T,ans;
int from[100005],next[100005],to[100005],f[100005],wg[100005];
int dis[350],degree[350],q[300000],h[350],pre[350];
bool b[350];
inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0'||y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0' && y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}
void ins(int x,int y,int flow,int w)
{
++len; from[len]=x; to[len]=y; f[len]=flow; wg[len]=w; next[len]=h[x]; h[x]=len;
}
void insert(int x,int y,int flow,int w)
{
ins(x,y,flow,w);
ins(y,x,0,-w);
}
bool spfa()
{
memset(dis,0x7f,sizeof(dis));
memset(b,true,sizeof(b)); b[S]=false;
dis[S]=0; int tail=1,head=0; q[tail]=S;
while (head<tail)
{
++head;
int u=h[q[head]];
while (u!=0)
{
if (f[u]>0 && wg[u]+dis[q[head]]<dis[to[u]])
{
pre[to[u]]=u;
dis[to[u]]=wg[u]+dis[q[head]];
if (b[to[u]])
{
b[to[u]]=false;
++tail; q[tail]=to[u];
}
}
u=next[u];
}
b[q[head]]=true;
}
if (dis[T]<1000000) return true;else return false;
}
void getans()
{
int now=T; int mx=0x7fffffff;
while (now!=S)
{
mx=min(mx,f[pre[now]]);
now=from[pre[now]];
}
now=T;
while (now!=S)
{
ans+=wg[pre[now]]*mx;
f[pre[now]]-=mx; f[pre[now]^1]+=mx;
now=from[pre[now]];
}
}
int main()
{
n=read();
S=n+1; SS=n+2; T=n+3; TT=n+4; len=1; ans=0;
insert(SS,1,INF,0);
for (int i=1;i<=n;++i)
{
insert(i,TT,INF,0);
int x=read();
for (int j=1;j<=x;++j)
{
int y=read(); int w=read(); ans+=w;
--degree[i];
++degree[y];
insert(i,y,INF,w);
}
}
for (int i=1;i<=n;++i)
{
if (degree[i]>0) insert(S,i,degree[i],0);
if (degree[i]<0) insert(i,T,-degree[i],0);
}
insert(TT,SS,INF,0);
while (spfa())
{
getans();
}
printf("%d",ans);
}
- bzoj 3876(有上下界的最小费用流)
- bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)
- 有下界的最小费用可行流(bzoj 3876: [Ahoi2014]支线剧情)
- BZOJ 3876 AHOI 2014 支线剧情 有上下界的费用流
- bzoj 3876: [Ahoi2014]支线剧情 (有上下界的费用流)
- BZOJ 3876 (流量有下界费用流)
- bzoj 2502(有上下界的最小流)
- 3876: [Ahoi2014]支线剧情|有上下界的费用流
- 3876: [Ahoi2014]支线剧情 有上下界的费用流
- [上下界有源汇最小费用可行流] BZOJ 3876: [Ahoi2014&Jsoi2014]支线剧情
- BZOJ 2055 80人环游世界 有上下界的费用流
- bzoj 2055: 80人环游世界 (有上下界的费用流)
- 【有上下界网络流+费用流多路增广】【bzoj 3876】: [Ahoi2014]支线剧情
- 网络流(最大流、最小费用最大流、有上下界的网络流)
- 【BZOJ2055】80人环游世界【有上下界的最小费用最大流】
- BZOJ 3876(支线剧情) 有下界费用流
- [BZOJ3876][AHOI2014]支线剧情-有下界的最小费用流
- BZOJ 3876([Ahoi2014]支线剧情-带下界的最小费用可行流)
- 69. Sqrt(x)
- POJ 2236-Wireless Network(并查集)
- nginx负载均衡配置
- iOS UIDevice- NSBundle - NSlocale
- 111-e-Minimum Depth of Binary Tree
- bzoj 3876(有上下界的最小费用流)
- 游戏人物 退出江湖
- 【再谈后缀自动机(入门)】[SPOJLCS2]Longest Common Substring II
- h264 NAL头解析
- 进程调度
- 喷水装置(一)t贪心算法
- 20160406servlet学习笔记验证码
- LeetCode 12 Integer to Roman
- NOJ-2076-天神小学-典型BFS题型