zoukankan      html  css  js  c++  java
  • hdu 3549 初试最大流问题

    Flow Problem

    Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 13315    Accepted Submission(s): 6372


    Problem Description
    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
     
    Input
    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
     
    Output
    For each test cases, you should output the maximum flow from source 1 to sink N.
     
    Sample Input
    2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
    #include<cstdio>
    #include<iostream>
    #include<vector>
    #include<cstring>
    #define maxn 16
    #define inf 1<<29
    using namespace std;
    struct edge
    {
        int to,cap,rev;
    };
    int n,m,vis[maxn];
    vector <edge> fuck[maxn];
    void add_edge(int from,int to,int cap)//构图过程
    {
        fuck[from].push_back((edge){to,cap,fuck[to].size()});//fuck[to].size() 表示反边在反向邻接表的位置--即fuck[to]中的位置
        fuck[to].push_back((edge){from,0,fuck[from].size()-1});//同上
    }
    void init()
    {
        for(int i=1;i<=n;i++) fuck[maxn].clear();
    }
    int dfs(int s,int e,int f)
    {
        if(s==e) return f;
        vis[s]=1;
        for(int i=0;i<fuck[s].size();i++)//每次的搜索 依次遍历s链接的点 更新cap 以及最大流量
        {
            edge &temp=fuck[s][i];//注意&这个东西  如果不用这个封装的话 改变的内容就只是形参的内容
            if(!vis[temp.to]&&temp.cap>0)
            {
                int d=dfs(temp.to,e,min(f,temp.cap));
                if(d>0)
                {
    temp.cap-=d; fuck[temp.to][temp.rev].cap+=d;// 回溯处理容量问题 以及对反向边的处理 //一直回溯到流量最小的那个点上去 然后去走其他路 return d;//回溯过程肯定是要更新的 } } } return 0; } int max_flow(int s,int e) { int flow=0; while(1)//不断的从s走到e这个点 当所有可能的通道cap为0的时候 贪心完成(因为每次过程都会cap进行一次更新) { memset(vis,0,sizeof(vis)); int temp=dfs(s,e,inf); if(temp==0) return flow; else flow+=temp; } } int main() { cin.sync_with_stdio(false); int t,Case=0; cin>>t; while(t--) { cin>>n>>m; init(); while(m--) { int a,b,c; cin>>a>>b>>c; add_edge(a,b,c);//构图 } cout<<"Case "<<++Case<<": "<<max_flow(1,n)<<endl; } return 0; }
     
     
     
     
  • 相关阅读:
    ExtJs学习笔记之ComboBox组件
    ExtJs学习笔记之学习小结LoginDemo
    ExtJs学习笔记之Button组件
    ExtJs学习笔记之TextField
    WAF指纹识别和XSS过滤器绕过技巧
    python中的迭代与递归
    使用Python对文档单词进行计数
    Python正则表达式使用实例
    Python十六进制与字符串的转换
    数组名a,数组名取地址&a,数组首地址&a[0],数组指针*p
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5810662.html
Copyright © 2011-2022 走看看