bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)
3931: [CQOI2015]网络吞吐量
题目:传送门
题解:
现在有点难受....跳了一个多钟...菜啊...
题意都把做法一起给了....最短路+网路流啊。
不想说话...记得开long long
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define qread(x)x=read();
using namespace std;
typedef long long LL;
const LL INF=(1LL<<);
inline int read()
{
int f=,x=;char ch;
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return f*x;
}
struct edge
{
int x,y,next;LL d;
}e[];int last1[],len1;
void ins1(int x,int y,LL d)
{
len1++;
e[len1].x=x;e[len1].y=y;e[len1].d=d;
e[len1].next=last1[x];last1[x]=len1;
}
struct node
{
int x,y,next,other;LL d;
}a[];int last[],len;
void ins(int x,int y,LL d)
{
int k1,k2;
len++;k1=len;
a[len].x=x;a[len].y=y;a[len].d=d;
a[len].next=last[x];last[x]=len; len++;k2=len;
a[len].x=y;a[len].y=x;a[len].d=;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
int n,m;
int st,ed,head,tail;
int list[],h[];
bool bt_h()
{
memset(h,,sizeof(h));h[st]=;
list[]=st;head=;tail=;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]== && a[k].d>)
{
h[y]=h[x]+;
list[tail++]=y;
}
}
head++;
}
if(h[ed]>)return true;
return false;
}
LL find_flow(int x,LL flow)
{
if(x==ed)return flow;
LL s=,t;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==(h[x]+) && a[k].d> && flow>s)
{
t=find_flow(y,min(a[k].d,flow-s));
s+=t;
a[k].d-=t;a[a[k].other].d+=t;
}
}
if(s==)h[x]=;
return s;
}
LL dd[];
bool v[];
void spfa()
{
for(int i=;i<=n;i++)dd[i]=INF;
memset(v,true,sizeof(v));
memset(list,,sizeof(list));
list[]=st;dd[st]=;head=;tail=;
while(head!=tail)
{
int x=list[head];
for(int k=last1[x];k;k=e[k].next)
{
int y=e[k].y;
if(dd[y]>dd[x]+e[k].d)
{
//printf("%d %d\n",x,y);
dd[y]=dd[x]+e[k].d;
if(v[y]==true)
{
v[y]=false;
list[tail]=y;tail++;if(tail==n+)tail=;
}
}
}
list[head]=;
head++;if(head==n+)head=;
v[x]=true;
}
}
int main()
{
//freopen("network.in","r",stdin);
//freopen("network.out","w",stdout);
qread(n);qread(m);
len1=;memset(last1,,sizeof(last));
len=;memset(last,,sizeof(last));
memset(list,,sizeof(list));
st=;ed=n*;
for(int i=;i<=m;i++)
{
int x,y;LL d;
qread(x);qread(y);scanf("%lld",&d);
ins1(x,y,d);
ins1(y,x,d);
//printf("%d %d %lld\n",x,y,d);
}
spfa();
memset(list,,sizeof(list));
for(int i=;i<=n;i++)
{
LL x;scanf("%lld",&x);
if(i!= && i!=n)
{
ins(i,i+n,x);
//printf("%d %d\n",i,i+n);
}
}
ins(st,+n,INF);
ins(n,ed,INF);
for(int i=;i<=n;i++)
{
for(int k=last1[i];k;k=e[k].next)
{
int y=e[k].y;
if(dd[i]+e[k].d==dd[y])
{
ins(i+n,y,INF);
}
}
}
LL ans=;
while(bt_h())
ans+=find_flow(st,INF);
printf("%lld\n",ans);
return ;
}
bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)的更多相关文章
- [CQOI2015]网络吞吐量(网络流+SPFA)
[CQOI2015]网络吞吐量 题目描述 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的 ...
- bzoj3931: [CQOI2015]网络吞吐量
将最短路图找出来,跑maxflow即可.有注意到数据范围.然后输出的时候%dWA了三次QAQ... #include<cstdio> #include<cstring> #in ...
- bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量
http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...
- 3931: [CQOI2015]网络吞吐量【网络流】
网络吞吐量(network)题目描述路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的地,路 ...
- [bzoj3931][CQOI2015]网络吞吐量——最短路+网络流
题目 传送门 题解 第一次一遍就AC一道bzoj上的题,虽然是一道水题... 我们做一边最短路,求出每个点的dist,然后再做一次类似spfa的操作,求出每个点是否可以用于建图. 在新图上拆点跑一边d ...
- BZOJ3931 [CQOI2015]网络吞吐量(最大流)
没啥好说的,有写过类似的,就是预处理出最短路上的边建容量网络. #include<cstdio> #include<cstring> #include<queue> ...
- 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量
跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...
- bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...
- 【BZOJ3931】[CQOI2015]网络吞吐量 最大流
[BZOJ3931][CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为 ...
随机推荐
- ZOJ 3288 Domination
D - Domination Time Limit:8000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Descr ...
- org.apache.hadoop.ipc.Client: Retrying connect to server
这个问题导致jps查看结点进程时发现找不到NodeManager或一段时间后消失,网上查找了很多博客,因hadoop版本不一样且出错的原因也可能不同,所以找了老半天. 步骤:jps --> 看l ...
- Oracle expdp导出多表或表中的部分数据
http://blog.itpub.net/16582684/viewspace-755072/
- javascript-知识点集合
第三课.JavaScript的语法与关键字 1.JavaScript的语法 字符串.数字.布尔.数组.对象.Null.Undefined 1.js的变量区分大小写 username userName ...
- How to Hide Zip Files Inside a Picture Without any Extra Software in Windows
http://www.howtogeek.com/119365/how-to-hide-zip-files-inside-a-picture-without-any-extra-software/ c ...
- JAVA设计模式之【状态模式】
状态模式 水.固态.气态.液态 账户.正常状态.透支状态.受限状态 状态模式中,用一个状态类来分散冗长的条件语句,让系统有灵活性和可扩展性 状态模式用于解决系统中复杂对象的状态转换以及不同状态下行为的 ...
- xBIM 基础01 简介
系列目录 [已更新最新开发文章,点击查看详细] 一.xBIM 简介 BIM(Building Information Modelling)建筑信息模型,xBIM(eXtensible Buil ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- if switch
一.基本if结构: 1.语法:if (条件){ 代码块 } 2.执行顺序:先判断条件,条件成立则行{}内的代码,不成立则跳出if结构快既不执行{}内的代码. 3.什么情况下要用基本if选择结构:当需要 ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...