hdu5889Barricade(最短路+网络流)

来源:互联网 发布:美国制造业数据 编辑:程序博客网 时间:2024/06/08 18:45

Barricade
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 219 Accepted Submission(s): 57

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

 青岛网络赛,题意:N个点M 条路径,每条路径长度为1,敌人从M节点点要进攻1节点,敌人总是选择最优路径即最短路径来进攻我方,为了阻止敌人,我们要把一些路封死,每条路径封死需要一些花费,求最小花费; 先建一个路径长度为都为1的图,跑两 遍最短路,分别是从1点出发的最短路和从N点出发的最短路,然后遍历所有边,如果该边是最短路上的路径着将该条边加入新图,意味着新图是所有的最短路径组成的图,流量为花费,然后求一遍最大流,得到最小割,最小割即为最小花费, 这道题目和hdu5294非常类似,都是最短路+最大流,不过这道题目第一个步骤用搜索应该也是可以过的
#include <iostream>#include <vector>#include <string.h>#include <stdio.h>#include <queue>using namespace std;const int maxn=2005;const int INF=0x3f3f3f3f;struct Edge{    int v;    int cost;    Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}};vector <Edge>E[maxn];void add(int u,int v,int w){    E[u].push_back(Edge(v,w));}bool vis [maxn];int cnt[maxn];int dist [maxn];bool spfa(int start,int n){    memset(vis,false,sizeof(vis));    for(int i=1; i<=n; i++)        dist[i]=INF;    vis[start]=true;    dist[start]=0;    queue<int>que;    while(!que.empty())        que.pop();    que.push(start);    memset(cnt,0,sizeof(cnt));    cnt[start]=1;    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=false;        for(int i=0;i<E[u].size();i++)        {            int v=E[u][i].v;            if(dist[v]>dist[u]+E[u][i].cost)            {                dist[v]=dist[u]+E[u][i].cost;                if(!vis[v])                {                    vis[v]=true;                    que.push(v);                    if(++cnt[v]>n)                        return false;                }            }        }    }    return true;}/////////////////////////////const int oo=1e9;const int mm=121111;const int mn=2999;int node,src,dest,edge;int ver[mm],flow[mm],_next[mm];int head[mn],work[mn],dis[mn],q[mn];void prepare(int _node,int _src ,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)        head[i]=-1;    edge=0;}void addedge(int u,int v,int c){    ver[edge]=v,flow[edge]=c,_next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,_next[edge]=head[v],head[v]=edge++;}bool Dicnic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)        dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=_next[i])        if(flow[i]&&dis[v=ver[i]]<0)        {            dis[q[r++]=v]=dis[u]+1;            if(v==dest)                return 1;        }        return 0;}int Dicnic_dfs(int u,int exp){    if(u==dest)        return exp;    for(int &i=work[u],v,tmp;i>=0;i=_next[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dicnic_dfs(v,min(exp,flow[i])))>0)    {        flow[i]-=tmp;        flow[i^1]+=tmp;        return tmp;    }  return 0;}int Dicnic_flow(){    int i,ret=0,delta;    while(Dicnic_bfs())    {        for(i=0;i<node;++i)            work[i]=head[i];        while(delta=Dicnic_dfs(src,oo))            ret+=delta;    }    return ret;}struct sa{   int u,v,w;}a[2005];int b[mn][3],dist2[mn];int main(){    int n,m,T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<=n;i++)            E[i].clear();        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);            add(a[i].u,a[i].v,1);            add(a[i].v,a[i].u,1);        }        spfa(1,n);        memcpy(dist2,dist,sizeof(dist));        //memset(dist,0,sizeof(dist));        spfa(n,n);        int k=0;        for(int i=0;i<m;i++)        {            if(dist2[a[i].u]>dist2[a[i].v])                swap(a[i].u,a[i].v);            if(dist[a[i].v]+1+dist2[a[i].u]==dist2[n])            {                b[k][1]=a[i].u;                b[k][0]=a[i].v;                b[k][2]=a[i].w;                k++;            }        }        prepare(n+1,1,n);        for(int i=0;i<k;i++)        {            addedge(b[i][1],b[i][0],b[i][2]);        }        int sum=Dicnic_flow();        for(int i=0;i<n;i++)            E[i].clear();        for(int i=0;i<k;i++)        {            add(b[i][1],b[i][0],b[i][2]);            add(b[i][0],b[i][1],b[i][2]);            //cout<<b[i][0]<<" "<<b[i][1]<<" "<<b[i][2]<<endl;        }        spfa(1,n);        printf("%d\n",sum);    }    return 0;}
0 0