HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 997    Accepted Submission(s): 306

Problem Description

The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general‘s castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible.

Input

The first line of input contains an integer t, then t test cases follow.
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.

Output

For each test cases, output the minimum wood cost.

Sample Input

1
4 4
1 2 1
2 4 2
3 1 3
4 3 4

Sample Output

4

Source

2016 ACM/ICPC Asia Regional Qingdao Online

Recommend

wange2014

Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5889

题目大意:

  N(N<=1000)个城市,你在城市1,敌人在城市N,敌人会选择从N到1的最短路进攻,你需要在某些边上放障碍来阻挡敌人进攻。

  总共有M(M<=1000)条无向边,连接两个城市,距离都为1,放置障碍的费用为wi。求最小费用。

题目思路:

  【BFS+最小割】

  首先因为每条边的距离都是1,所以先从N开始往1跑最短路,扩展所有距离d[u]<=d[1]的点,在最短路过程中,对于u->v的边,在新图上加一条v->u的容量为wi的边。

  这样从N跑完一次BFS之后建的新图是从1到N的一张最短路图。问题转化为求新图的最小割。从1到N开始跑最大流即可。

  1 //
  2 //by coolxxx
  3 //#include<bits/stdc++.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 //#include<stdbool.h>
 19 #include<math.h>
 20 #define min(a,b) ((a)<(b)?(a):(b))
 21 #define max(a,b) ((a)>(b)?(a):(b))
 22 #define abs(a) ((a)>0?(a):(-(a)))
 23 #define lowbit(a) (a&(-a))
 24 #define sqr(a) ((a)*(a))
 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define eps (1e-10)
 28 #define J 10000
 29 #define mod 1000000007
 30 #define MAX 0x7f7f7f7f
 31 #define PI 3.14159265358979323
 32 #pragma comment(linker,"/STACK:1024000000,1024000000")
 33 #define N 1004
 34 #define M 10004
 35 using namespace std;
 36 typedef long long LL;
 37 double anss;
 38 LL aans;
 39 int cas,cass;
 40 int n,m,lll,ans;
 41 int nn,S,T;
 42 int d[N],vd[N],last[N],last1[N];
 43 bool u[N];
 44 struct xxx
 45 {
 46     int next,to,q;
 47 }a[M<<1],b[M<<1];
 48 void add(int x,int y,int z)
 49 {
 50     a[++lll].next=last[x];
 51     a[lll].to=y;
 52     a[lll].q=z;
 53     last[x]=lll;
 54 }
 55 void link(int x,int y,int z)
 56 {
 57     b[++cas].next=last1[x];
 58     b[cas].to=y;
 59     b[cas].q=z;
 60     last1[x]=cas;
 61 }
 62 int sap(int u,int f)
 63 {
 64     int i,v,tt,asp=0,mix=nn-1;
 65     if(u==T)return f;
 66     for(i=last[u];i;i=a[i].next)
 67     {
 68         v=a[i].to;
 69         if(a[i].q>0)
 70         {
 71             if(d[u]==d[v]+1)
 72             {
 73                 tt=sap(v,min(f-asp,a[i].q));
 74                 asp+=tt;
 75                 a[i].q-=tt;
 76                 a[i^1].q+=tt;
 77                 if(asp==f || d[S]==nn)
 78                     return asp;
 79             }
 80             mix=min(mix,d[v]);
 81         }
 82     }
 83     if(asp!=0)return asp;
 84     if(!--vd[d[u]])d[S]=nn;
 85     else vd[d[u]=mix+1]++;
 86     return asp;
 87 }
 88 void bfs()
 89 {
 90     int now,to,i;
 91     queue<int>q;
 92     mem(d,MAX);
 93     u[n]=1;q.push(n);d[n]=0;
 94     while(!q.empty())
 95     {
 96         now=q.front();q.pop();
 97         if(d[now]>=d[S])continue;
 98         for(i=last1[now];i;i=b[i].next)
 99         {
100             to=b[i].to;
101             if(d[now]+1>d[to])continue;
102             d[to]=d[now]+1;
103             add(now,to,0),add(to,now,b[i].q);
104             if(!u[to])
105             {
106                 u[to]=1;
107                 if(to!=S)q.push(to);
108             }
109         }
110     }
111     mem(d,0);
112 }
113 int main()
114 {
115     #ifndef ONLINE_JUDGEW
116     freopen("1.txt","r",stdin);
117 //    freopen("2.txt","w",stdout);
118     #endif
119     int i,j,k;
120     int x,y,z,f;
121 //    init();
122     for(scanf("%d",&cass);cass;cass--)
123 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
124 //    while(~scanf("%s",s))
125 //    while(~scanf("%d",&n))
126     {
127         lll=cas=1;ans=0;
128         mem(u,0);mem(vd,0);mem(last,0);mem(last1,0);
129         scanf("%d%d",&n,&m);
130         for(i=1;i<=m;i++)
131         {
132             scanf("%d%d%d",&x,&y,&z);
133             link(x,y,z),link(y,x,z);
134         }
135         nn=n;
136         S=1,T=n;
137         vd[0]=nn;
138         bfs();
139         while(d[S]<nn)
140         {
141             f=sap(S,MAX);
142             ans+=f;
143         }
144         printf("%d\n",ans);
145     }
146     return 0;
147 }
148 /*
149 //
150
151 //
152 */

时间: 2024-10-15 18:39:30

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)的相关文章

2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 406 Problem Description I will show you the most popular board game in the Shanghai Ingress Resis

HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 309 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

HDU 5889 Barricade (bfs + 最小割)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each

【2016 ACM/ICPC Asia Regional Qingdao Online】

[ HDU 5878 ] I Count Two Three 考虑极端,1e9就是2的30次方,3的17次方,5的12次方,7的10次方. 而且,不超过1e9的乘积不过5000多个,于是预处理出来,然后每次二分找就可以了. /* TASK:I Count Two Three 2^a*3^b*5^c*7^d的最小的大于等于n的数是多少 LANG:C++ URL:http://acm.hdu.edu.cn/showproblem.php?pid=5878 */ #include <iostream>

2016 ACM/ICPC Asia Regional Qingdao Online

吐槽: 群O的不是很舒服 不知道自己应该干嘛 怎样才能在团队中充分发挥自己价值 一点都不想写题 理想中的情况是想题丢给别人写 但明显滞后 一道题拖沓很久 中途出岔子又返回来搞 最放心的是微软微软妹可以随便丢 有几个小盆友也比较靠谱 还有几个小盆友一开始有点担心 后来都做的挺棒 写题的选择上很尴尬 会写的别人也会 要么队内都不会 结果大概是写了一些板子题 感觉到了比较艰难的阶段 有些题是要我学着去写的 不会写没有突破 1001 I Count Two Three AC by ctr 指数不会很大,

hdu 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878 题目大意: 给出一个数n ,求一个数X, X>=n. X 满足一个条件 X= 2^a*3^b*5^c*7^d 求靠近n的X值. 解题思路: 打表+二分查找 [切记用 cin cout,都是泪...] AC Code: 1 #include<bits/stdc++.h> 2 using namespace std; 3 long long na[100002]; 4 5 int main

2016 ACM/ICPC Asia Regional Qingdao Online HDU5889

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 解法:http://blog.csdn.net/u013532224/article/details/46992973 然后改改模版 #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <str

2016 ACM/ICPC Asia Regional Qingdao Online HDU5883

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 解法:先判断是不是欧拉路,然后枚举 #pragma comment(linker, "/STACK:102400000,102400000") #include <math.h> #include <time.h> #include <stdio.h> #include <string.h> #include <stdlib.h>

2016 ACM/ICPC Asia Regional Qingdao Online HDU5882

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5882 解法:一个点必须出度和入度相同就满足题意,所以加上本身就是判断奇偶性 #include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<set> #include<queue> #include<vector> #include<