hdu5889(最短路+最小割)

emmmm这个跑个最短路。。然后dfs跑出最短路径树直接把树上的边都拿来重建图然后跑最小割就可以了。。。

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1005
#define nm 20005
#define N 40005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}




struct edge{int t,v;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM],*tmp[NM];
void _add(int x,int y,int v){o->v=v;o->t=y;o->next=h[x];h[x]=o++;}
void add(int x,int y,int v){_add(x,y,v);_add(y,x,0);h[x]->rev=h[y];h[y]->rev=h[x];}

struct TMP{
    int x,v;
    bool operator<(const TMP&o)const{return v>o.v;}
};
struct TM{int x,y,v;}a[nm];

int n,m,d[NM],cnt[NM];
bool v[NM];
priority_queue<TMP>q;

int maxflow(){
    int flow=0,tot=n;edge*j;
    mem(d);mem(cnt);mem(p);mem(tmp);
    inc(i,1,n)tmp[i]=h[i];
    cnt[0]=tot;
    for(int x=1,s=inf;d[x]<tot;){
	for(j=tmp[x];j;j=j->next)if(j->v&&d[j->t]+1==d[x])break;
	if(j){
	    s=min(s,j->v);tmp[x]=p[j->t]=j;
	    if((x=j->t)==n){
		for(;x>1;x=p[x]->rev->t)p[x]->v-=s,p[x]->rev->v+=s;
		flow+=s;s=inf;
	    }
	}else{
	    if(!--cnt[d[x]])break;d[x]=tot;
	    link(x)if(j->v&&d[x]>d[j->t]+1)tmp[x]=j,d[x]=d[j->t]+1;
	    cnt[d[x]]++;
	    if(p[x])x=p[x]->rev->t;
	}
    }
    return flow;
}

void dij(){
    inc(i,2,n)d[i]=inf;q.push(TMP{1,0});
    while(!q.empty()){
	int t=q.top().x;q.pop();
	link(t)if(d[j->t]>d[t]+j->v)q.push(TMP{j->t,d[j->t]=d[t]+j->v});
    }
}

bool dfs(int x){
    if(x==n)return v[x]=true;
    bool f=false;
    link(x)if(d[j->t]==d[x]+j->v&&(v[j->t]||dfs(j->t)))v[x]=f=true;
    return f;
}

int main(){
    //freopen("data.in","r",stdin);
    int _=read();while(_--){
	mem(e);mem(h);o=e;mem(d);mem(v);
	n=read();m=read();
	inc(i,1,m){a[i].x=read();a[i].y=read();a[i].v=read();_add(a[i].x,a[i].y,1);_add(a[i].y,a[i].x,1);}
	dij();
	if(!dfs(1)){printf("0\n");continue;}
	mem(e);mem(h);o=e;
	inc(i,1,m)if(v[a[i].x]&&v[a[i].y]){
	    if(d[a[i].y]==d[a[i].x]+1)add(a[i].x,a[i].y,a[i].v);
	    swap(a[i].x,a[i].y);
	    if(d[a[i].y]==d[a[i].x]+1)add(a[i].x,a[i].y,a[i].v);
	}
	printf("%d\n",maxflow());
    }
    return 0;
}

Barricade

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


 

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   |   We have carefully selected several similar problems for you:  6373 6372 6371 6370 6369 

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/81546460
今日推荐