2016青岛网络赛 Barricade
Barricade
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.
4 4
1 2 1
2 4 2
3 1 3
4 3 4
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <cstring>
- #include <string>
- #include <set>
- #include <map>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <list>
- #define rep(i,m,n) for(i=m;i<=n;i++)
- #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
- #define mod 1000000007
- #define inf 0x3f3f3f3f
- #define vi vector<int>
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define ll long long
- #define pi acos(-1.0)
- #define pii pair<int,int>
- #define Lson L, mid, rt<<1
- #define Rson mid+1, R, rt<<1|1
- const int maxn=1e3+;
- using namespace std;
- ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
- ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
- int n,m,k,t,h[maxn],tot,vis[maxn],s,cur[maxn],f[maxn],d[maxn],g[maxn];
- vi edge[maxn];
- struct Node
- {
- int x,y,z;
- Node(){}
- Node(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
- }op[];
- struct node
- {
- int to,nxt,cap,flow;
- }e[<<];
- void add(int x,int y,int z)
- {
- e[tot].to=y;
- e[tot].nxt=h[x];
- e[tot].cap=z;
- e[tot].flow=;
- h[x]=tot++;
- e[tot].to=x;
- e[tot].nxt=h[y];
- e[tot].flow=;
- h[y]=tot++;
- }
- bool bfs()
- {
- memset(vis,,sizeof vis);
- queue<int>p;
- p.push(s);
- vis[s]=;
- while(!p.empty())
- {
- int x=p.front();p.pop();
- for(int i=h[x];i!=-;i=e[i].nxt)
- {
- int to=e[i].to,cap=e[i].cap,flow=e[i].flow;
- if(!vis[to]&&cap>flow)
- {
- vis[to]=vis[x]+;
- p.push(to);
- }
- }
- }
- return vis[t];
- }
- void pr_bfs(int s)
- {
- int i;
- memset(d,inf,sizeof d);
- memset(vis,,sizeof vis);
- queue<int>p;p.push(s);vis[s]=;d[s]=;
- while(!p.empty())
- {
- int q=p.front();p.pop();vis[q]=;
- for(int x:edge[q])
- {
- if(d[x]>d[q]+)
- {
- d[x]=d[q]+;
- if(!vis[x])p.push(x),vis[x]=;
- }
- }
- }
- if(s==n)rep(i,,n)f[i]=d[i];
- else rep(i,,n)g[i]=d[i];
- return;
- }
- int dfs(int x,int a)
- {
- if(x==t||a==)return a;
- int ans=,j;
- for(int&i=cur[x];i!=-;i=e[i].nxt)
- {
- int to=e[i].to,cap=e[i].cap,flow=e[i].flow;
- if(vis[to]==vis[x]+&&(j=dfs(to,min(a,cap-flow)))>)
- {
- e[i].flow+=j;
- e[i^].flow-=j;
- ans+=j;
- a-=j;
- if(a==)break;
- }
- }
- return ans;
- }
- int max_flow(int s,int t)
- {
- int flow=,i;
- while(bfs())
- {
- memcpy(cur,h,sizeof cur);
- flow+=dfs(s,inf);
- }
- return flow;
- }
- int main()
- {
- int i,j,test;
- scanf("%d",&test);
- while(test--)
- {
- tot=;
- memset(h,-,sizeof h);
- scanf("%d%d",&n,&m);
- rep(i,,n)edge[i].clear();
- rep(i,,m-)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- op[i]=Node(a,b,c);
- edge[a].pb(b),edge[b].pb(a);
- }
- pr_bfs(n);
- pr_bfs();
- rep(i,,m-)
- {
- int a=op[i].x,b=op[i].y,c=op[i].z;
- if(f[a]+g[b]+==f[])add(a,b,c);
- if(f[b]+g[a]+==f[])add(b,a,c);
- }
- s=n,t=;
- printf("%d\n",max_flow(s,t));
- }
- //system("Pause");
- return ;
- }
2016青岛网络赛 Barricade的更多相关文章
- HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)
题目链接 2016 青岛网络赛 Problem C 题意 给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...
- HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)
题目链接 2016 Qingdao Online Problem I 题意 在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...
- HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)
I Count Two Three 31.1% 1000ms 32768K I will show you the most popular board game in the Shanghai ...
- HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)
Herbs Gathering 10.76% 1000ms 32768K Collecting one's own plants for use as herbal medicines is pe ...
- HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)
背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...
- HDU5880 Family View(2016青岛网络赛 AC自动机)
题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...
- 2016青岛网络赛 Sort
Sort Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Des ...
- 2016青岛网络赛 The Best Path
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Pr ...
- 2016 年青岛网络赛---Family View(AC自动机)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...
随机推荐
- 高级CSS
target="_black"新窗口打开,且打开第二个链接出来的页面覆盖第一个页面 .ui-pg-table td[dir="ltr"]{*width:94px ...
- kloxo面板教程-折腾了一天
------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...
- find排除目录
在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用. 命令语法: ...
- Python使用os.listdir()函数来获得目录中的内容
摘自:http://it.100xuexi.com/view/otdetail/20130423/057606dc-7ad1-47e4-8ea6-0cf75f514837.html 1.在Pyth ...
- Html:upload
文件上传框 有时候,需要用户上传自己的文件,文件上传框看上去和其它 文本域差不多,只是它还包含了一个浏览按钮.访问者可以通 过输入需要上传的文件的路径或者点击浏览按钮选择需要上传 的文件. 代码格式: ...
- Xcode6无法用xib得问题解决方法
1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...
- Nginx配置文件nginx.conf中文详解【转】
PS:Nginx使用有两三年了,现在经常碰到有新用户问一些很基本的问题,我也没时间一一回答,今天下午花了点时间,结合自己的使用经验,把Nginx的主要配置参数说明分享一下,也参考了一些网络的内容,这篇 ...
- GsonFormat 报错
GsonFormat原来也有bug 我是用GsonFormat来生成java bean的,但是运行起来居然报 Caused by: java.lang.NumberFormatException: E ...
- PatrolRobot(UVa1600)BFS
PatrolRobot(UVa1600)BFS 珉黻郐距 河吏蝉醉 闵棵黏言 芤她之瞌 褰上稽莨 錾傻奉 郦玫睃芩 摇摇头还没回答魏海洪就抢先回答道:呵呵你们几个别试 蚰镉氡 钬 绦可 ...
- ASP.NET 会话状态的模式
ASP.NET 会话状态为会话数据提供了几个不同的存储选项.每个选项都通过一个 SessionStateMode 枚举值进行识别.如下列表中描述了可用的会话状态模式: InProc 模式:把会话状态存 ...