zoukankan      html  css  js  c++  java
  • HDU(2485),最小割最大流

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485

    Destroying the bus stations

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2651    Accepted Submission(s): 891


    Problem Description
    Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station.
    No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.


    Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.
     
    Input
    There are several test cases. Input ends with three zeros.

    For each test case:

    The first line contains 3 integers, n, m and k. (0< n <=50, 0< m<=4000, 0 < k < 1000)
    Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.
     
    Output
    For each test case, output the minimum number of stations Gabiluso must destroy.
     
    Sample Input
    5 7 3 1 3 3 4 4 5 1 2 2 5 1 4 4 5 0 0 0
     
    Sample Output
    2
     
    Source
     
    题意:

    给定n个点, m条有向边 ,k

    下面m条有向边

    问删最少几个点使得1-n的最短路>k

    分析:
    其证明还没看懂,先做了再说咯。证明在紫书370,写一下结论:在增广路算法结束时,f是s-t最大流,(S,T)是最小割。
    然后问了一下阳哥,记录几个结论,最大流=最小割(边)=最小割(点)。
    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <vector>
    #include <queue>
    #define INF 0x3f3f3f3f
    using namespace std;
    const int maxn = 4000 + 10;
    int k;
    
    struct Edge
    {
        int from,to,cap,flow,cost;
        Edge() {}
        Edge(int a,int b,int c,int d,int e):from(a),to(b),cap(c),flow(d),cost(e) {}
    };
    
    struct MCMF
    {
        int n,m,s,t;
        vector<Edge> edges;
        vector<int> g[maxn];
        int inq[maxn];
        int d[maxn];
        int p[maxn];
        int a[maxn];
    
        void init(int n)
        {
            this->n =n;
            for(int i=0; i<n; i++)g[i].clear();
            edges.clear();
        }
        void addedge(int from,int to,int cap,int cost)
        {
            Edge e1= Edge(from,to,cap,0,cost), e2= Edge(to,from,0,0,-cost);
            edges.push_back(e1);
            edges.push_back(e2);
            m=edges.size();
            g[from].push_back(m-2);
            g[to].push_back(m-1);
        }
        bool spfa(int s,int t, int & flow,int & cost)
        {
            for(int i=0; i<n; i++)
                d[i]=INF;
            memset(inq,0,sizeof(inq));
            d[s]=0;
            inq[s]=1;
            p[s]=0;
            a[s]=INF;
            queue<int>q;
            q.push(s);
            while(!q.empty())
            {
                int u=q.front();
                q.pop();
                inq[u]=0;
                for(int i=0; i<g[u].size(); i++)
                {
                    Edge & e = edges[g[u][i]];
                    if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                    {
                        d[e.to]=d[u]+e.cost;
                        p[e.to]=g[u][i];
                        a[e.to]=min(a[u],e.cap-e.flow);
                        if(!inq[e.to])
                        {
                            q.push(e.to);
                            inq[e.to]=1;
                        }
                    }
                }
            }
            if(d[t]>k)
                return false;
            if(d[t]==INF)
                return false;
    
            flow+=a[t];
            cost+=a[t]*d[t];
            for(int u=t; u!=s; u=edges[p[u]].from)
            {
                edges[p[u]].flow +=a[t];
                edges[p[u]^1].flow-=a[t];
            }
            return true;
        }
    
        int  MincostMaxflow(int s,int t)
        {
            int flow=0,cost =0;
            while(spfa(s,t,flow,cost));
            return flow;
        }
    } sol;
    
    
    int main()
    {
        freopen("input.txt","r",stdin);
        int n,m;
        while(scanf("%d%d%d",&n,&m,&k))
        {
            int s = 0,t = 2*n+1;
            if(n==0&&m==0&&k==0) break;
            int u,v;
            sol.init(n*2+2);
            for(int i=1; i<=n; i++)
                sol.addedge(i+n,i,1,0);
    
            sol.addedge(1,1+n,INF,0);
            sol.addedge(n,2*n,INF,0);
            sol.addedge(0,1,INF,0);
            sol.addedge(2*n,t,INF,0);
            for(int i=0; i<m; i++)
            {
                scanf("%d%d",&u,&v);
                sol.addedge(u,v+n,INF,1);
            }
            printf("%d
    ",sol.MincostMaxflow(s,t));
        }
        return 0;
    }
  • 相关阅读:
    Python 魔术方法及调用方式
    Python metaclasses
    Python 实例方法,类方法和静态方法
    Python 2.x和3.x不同点
    Python 类总结
    Python 添加模块
    Python-读入json文件并进行解析及json基本操作
    linux环境下的python安装过程
    Linux系统目录结构以及简单说明
    linux下python安装
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5806126.html
Copyright © 2011-2022 走看看