2016青岛网络赛 Barricade

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
分析:对最短路求最小割最大流即可;
代码:
#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 ;
}
 
上一篇:pku1273 Drainage Ditches


下一篇:编程界失传秘术,SSO单点登录,什么是单点,如何实现登录?