bzoj 3876: [Ahoi2014]支线剧情
2024-10-08 17:20:07
就是加一个1的下界就好了。
#include<bits/stdc++.h>
#define N 100005
#define LL long long
#define inf 0x3f3f3f3f
#define ls tr[x][0]
#define rs tr[x][1]
using namespace std;
inline int ra()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
struct node{
int c,v,from,to,next;
}e[N];
int tot=,ans,from[N],n,m,S,T,head[N],cnt,dis[N],q[N<<];
bool inq[N];
void ine(int x, int y, int v, int c)
{
e[++tot].to=y; e[tot].next=head[x]; head[x]=tot;
e[tot].v=v; e[tot].c=c; e[tot].from=x;
}
void insert(int x, int y, int c, int v)
{
ine(x,y,v,c); ine(y,x,-v,);
}
bool spfa()
{
for (int i=; i<=n+; i++) dis[i]=inf;
int l=,r=; q[]=S; dis[S]=; inq[S]=;
while (l<r)
{
int x=q[l++];
for (int i=head[x];i;i=e[i].next)
if (dis[e[i].to]>dis[x]+e[i].v && e[i].c)
{
dis[e[i].to]=dis[x]+e[i].v;
from[e[i].to]=i;
if (!inq[e[i].to])
{
inq[e[i].to]=;
q[r++]=e[i].to;
}
}
inq[x]=;
}
if (dis[T]==inf) return ;
return ;
}
void mcf()
{
int x=inf;
for (int i=from[T];i;i=from[e[i].from]) x=min(x,e[i].c);
for (int i=from[T];i;i=from[e[i].from]) ans+=x*e[i].v,e[i].c-=x,e[i^].c+=x;
}
void fyl()
{
while (spfa()) mcf();
}
int main()
{
n=ra();
S=n+; T=n+;
for (int i=; i<=n; i++)
{
int cnt=ra();
insert(i,T,cnt,);
if (i!=) insert(i,,inf,);
for (int j=; j<=cnt; j++)
{
int x=ra(),v=ra();
insert(S,x,,v);
insert(i,x,inf,v);
}
}
fyl();
cout<<ans;
return ;
}
最新文章
- ZK 页面间参数传递
- MVCC PostgreSQL实现事务和多版本并发控制的精华
- 几个有用的JavaScript/jQuery代码片段(转)
- 为什么for(int i=0;i<;9;i++) 在c语言中是错误的?
- django rest_framework
- python 三元运算符
- 用Volley让GridView加载网络图片
- 织梦dedecms后台添加图片style全部都变成st<;x>;yle的解决办法
- #pragma的用法
- 【Tips】Endnote导入IEEE Xplore文献方法《转载》
- ACM/ICPM2014鞍山现场赛D Galaxy (HDU 5073)
- .net 报错汇总——持续更新
- nginx配置location总结及rewrite规则写法(2)
- 将自己的域名解析跳转到博客主页(GitHub中的gitpage跳转)
- [转CSDN多篇文章]WEB 3D SVG CAD 矢量 几种实现方案
- Chrome格式化JavaScript代码
- Expo大作战(十八)--expo如何发布成独立应用程序,打包成apk或者ipa,发布到对应应用商店
- Redis源码阅读(二)高可用设计——复制
- UEditor 的使用
- [Cubieboard] Node.js 在 Lubuntu 上安装指南