【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量
跑出最短路图,然后把结点拆点跑最大流。
- #include<cstdio>
- #include<queue>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define M 100001
- #define INF 2147483647
- #define N 501
- typedef long long ll;
- namespace Net
- {
- int v[M<<1],next[M<<1],first[N<<1],en,cap[M<<1];
- queue<int>q;
- void AddEdge(int U,int V,int Cap)
- {
- v[en]=V; cap[en]=Cap; next[en]=first[U]; first[U]=en++;
- v[en]=U; next[en]=first[V]; first[V]=en++;
- }
- int S,T,n;
- int d[N<<1],cur[N<<1];
- int bfs()
- {
- memset(d,-1,sizeof(int)*(n+1));
- d[S]=0;
- q.push(S);
- while(!q.empty())
- {
- int U=q.front(); q.pop();
- for(int i=first[U];i!=-1;i=next[i])
- if(cap[i]&&d[v[i]]==-1)
- {
- d[v[i]]=d[U]+1;
- q.push(v[i]);
- }
- }
- return d[T]!=-1;
- }
- int dfs(int U,int a)
- {
- if(U==T||(!a))
- return a;
- int f,Flow=0;
- for(int &i=cur[U];i!=-1;i=next[i])
- if(d[v[i]]==d[U]+1&&(f=dfs(v[i],min(a,cap[i]))))
- {
- cap[i]-=f; cap[i^1]+=f;
- Flow+=f; a-=f;
- if(!a) break;
- }
- if(!Flow) d[U]=-1;
- return Flow;
- }
- ll MaxFlow()
- {
- ll Flow=0;
- int tmp;
- while(bfs())
- {
- memcpy(cur,first,sizeof(int)*(n+1));
- while(tmp=dfs(S,INF))
- Flow+=(ll)tmp;
- }
- return Flow;
- }
- };
- namespace SP
- {
- int n,m;
- queue<int>q;
- int v[M<<1],w[M<<1],next[M<<1],first[N],en;
- void AddEdge(int U,int V,int W)
- {
- v[++en]=V;
- w[en]=W;
- next[en]=first[U];
- first[U]=en;
- }
- ll d[N];
- bool inq[N];
- void spfa(int S)
- {
- for(int i=1;i<=n;++i)
- d[i]=INF;
- d[S]=0;
- q.push(S);
- inq[S]=1;
- while(!q.empty())
- {
- int U=q.front();
- for(int i=first[U];i;i=next[i])
- if(d[U]+(ll)w[i]<d[v[i]])
- {
- d[v[i]]=d[U]+(ll)w[i];
- if(!inq[v[i]])
- {
- inq[v[i]]=1;
- q.push(v[i]);
- }
- }
- q.pop(); inq[U]=0;
- }
- }
- };
- int Map(int x,bool op)
- {
- if(x==Net::S||x==Net::T)
- return x;
- return ((!op)?((x<<1)-2):((x<<1)-1));
- }
- int main()
- {
- int x,y,z;
- scanf("%d%d",&SP::n,&SP::m);
- for(int i=1;i<=SP::m;++i)
- {
- scanf("%d%d%d",&x,&y,&z);
- SP::AddEdge(x,y,z);
- SP::AddEdge(y,x,z);
- }
- SP::spfa(1);
- Net::S=1;
- Net::T=2*SP::n-2;
- Net::n=2*SP::n-2;
- memset(Net::first,-1,sizeof(int)*(Net::n+1));
- for(int i=1;i<=SP::en;i+=2)
- {
- if(SP::d[SP::v[i+1]]+(ll)SP::w[i]==SP::d[SP::v[i]])
- Net::AddEdge(Map(SP::v[i+1],1),Map(SP::v[i],0),INF);
- if(SP::d[SP::v[i]]+(ll)SP::w[i]==SP::d[SP::v[i+1]])
- Net::AddEdge(Map(SP::v[i],1),Map(SP::v[i+1],0),INF);
- }
- scanf("%d",&x);
- for(int i=2;i<SP::n;++i)
- {
- scanf("%d",&x);
- Net::AddEdge(Map(i,0),Map(i,1),x);
- }
- scanf("%d",&x);
- cout<<Net::MaxFlow()<<endl;
- return 0;
- }
【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量的更多相关文章
- bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量
http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...
- bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)
3931: [CQOI2015]网络吞吐量 题目:传送门 题解: 现在有点难受....跳了一个多钟...菜啊... 题意都把做法一起给了....最短路+网路流啊. 不想说话...记得开long lon ...
- bzoj3931: [CQOI2015]网络吞吐量
将最短路图找出来,跑maxflow即可.有注意到数据范围.然后输出的时候%dWA了三次QAQ... #include<cstdio> #include<cstring> #in ...
- BZOJ3931 [CQOI2015]网络吞吐量(最大流)
没啥好说的,有写过类似的,就是预处理出最短路上的边建容量网络. #include<cstdio> #include<cstring> #include<queue> ...
- [bzoj3931][CQOI2015]网络吞吐量——最短路+网络流
题目 传送门 题解 第一次一遍就AC一道bzoj上的题,虽然是一道水题... 我们做一边最短路,求出每个点的dist,然后再做一次类似spfa的操作,求出每个点是否可以用于建图. 在新图上拆点跑一边d ...
- 【BZOJ3931】[CQOI2015]网络吞吐量 最大流
[BZOJ3931][CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为 ...
- 【BZOJ-3931】网络吞吐量 最短路 + 最大流
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1228 Solved: 524[Submit][Stat ...
- BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )
最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...
- BZOJ 3931: [CQOI2015]网络吞吐量 最大流
3931: [CQOI2015]网络吞吐量 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
随机推荐
- MySQL的字符集小结
正确了解MySQL的字符集问题,能够从根本上解决乱码的困扰. 首先,MySQL的字符集问题主要是两个概念,一个是Character Sets,一个是Collations,前者是字符内容及编码,后者是对 ...
- 对zip文件进行解压操作和对一个文件进行压缩操作
注意这里用的是apche下的zip package org.springframework.validation; import org.apache.tools.zip.ZipEntry; impo ...
- linux 学习好资源
Linux-Wiki.cn http://linux-wiki.cn/wiki/zh-hans/Linux%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84 Linux目录 ...
- 知问前端——按钮UI
按钮(button),可以给生硬的原生按钮或者文本提供更多丰富多彩的外观.它不单单可以设置按钮或文本,还可以设置单选按钮和多选按钮. 使用button按钮 使用button按钮UI的时候,不一定必须是 ...
- 【BZOJ】5010: [Fjoi2017]矩阵填数
[算法]离散化+容斥原理 [题意]给定大矩阵,可以每格都可以任意填1~m,给定n个子矩阵,要求满足子矩阵内的最大值为vi,求方案数. n<=10,h,w<=1w. [题解] 此题重点之一在 ...
- sender的作用
https://www.evernote.com/shard/s227/sh/c2441a07-6b7e-4659-8452-9f768ee9cc66/73a115ed352421e10629 ...
- 【洛谷 P2346】四子连棋(状态压缩,搜索)
其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...
- HDU 2089 不要62 (数学)
题目链接 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了 ...
- python3 json、logging、sys模块
json模块 import json dic = {'name':'egon','age':32} # ------------------------------>序列化 f = open(' ...
- Dancing Links [Kuangbin带你飞] 模版及题解
学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/762786 ...