HDU 5889 Barricade 2016青岛网赛

47 篇文章 0 订阅
10 篇文章 0 订阅
Barricade
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1171    Accepted Submission(s): 355


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

在最短路里找最小割
边长度都相同 所以最短路可以BFS找 然后用最短路上的边构图
找最小割跑一遍最大流即可
注意构图的时候 不要把边重复添加进去
PS:虽然原图是无向图 但是如果u->v是最短路上的边 只需要添加u->v 不需要v->u(在此无限WA)

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
//#define CHECK_TIME

const int N=1000+3;
const int M=10003;

struct edge{//终点,流量,反向边在G[to]中的下标
    int to,cap,rev;
};
struct edge2{//起点,终点,流量
    int fr,to,cap;
};

vector<edge>G[N];//最短路构造的图
edge2 E[M];//保存所有边 只保存单向
vector<pii>G_pre[N];//原图 跑最短路
bool used[N];//DFS标记
int dis1[N];//从1出发跑最短路的距离
int dis2[N];//从n出发跑最短路的距离

inline void add_edge(int fr,int to,int cap){//在G中添加一条边
    G[fr].push_back({to,cap,G[to].size()});
    G[to].push_back({fr,0,G[fr].size()-1});
}

int dfs(int v,int t,int f){
    if(v==t)
        return f;
    used[v]=true;
    for(int i=0;i<G[v].size();++i){
        edge&e=G[v][i];
        if(used[e.to]==false&&e.cap>0){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){//求最大流
    int flow=0;
    while(true){
        fill(used,used+N+1,false);
        int f=dfs(s,t,INF);
        if(f==0)
            return flow;
        flow+=f;
    }
}

void bfs(int s,int*dis){
    fill(dis,dis+N,INF);
    deque<int>de{s};
    dis[s]=0;
    while(!de.empty()){
        int fr=de.front();
        de.pop_front();;
        for(int i=0;i<G_pre[fr].size();++i){
            int to=G_pre[fr][i].first;
            if(dis[to]>dis[fr]+1){
                dis[to]=dis[fr]+1;
                de.push_back(to);
            }
        }
    }
}

void shortest_road(int n,int m){//用最短路构图
    for(int i=1;i<=m;++i){
        int u=E[i].fr,v=E[i].to;
        if(abs(dis1[E[i].fr]-dis1[E[i].to])==1){
//如果从1出发到u的距离+从n出发到v的距离+1=1到n的距离 u,v之间的边是最短路
            if(dis1[u]+dis2[v]+1==dis1[n])
                add_edge(E[i].fr,E[i].to,E[i].cap);
            if(dis1[v]+dis2[u]+1==dis1[n])
                add_edge(E[i].to,E[i].fr,E[i].cap);
        }
    }
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int T,n,m,fr,to,cap;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
            G_pre[i].clear();
        for(int i=1;i<=m;++i){
            scanf("%d%d%d",&fr,&to,&cap);
            E[i]={fr,to,cap};
            G_pre[fr].push_back({to,cap});
            G_pre[to].push_back({fr,cap});
        }
        bfs(1,dis1);
        bfs(n,dis2);
        if(dis1[n]==INF){
            printf("0\n");
            continue;
        }
        for(int i=1;i<=n;++i)
            G[i].clear();
        shortest_road(n,m);
        printf("%d\n",max_flow(1,n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值