hdu3549 Flow Problem

来源:互联网 发布:淡斑 知乎 编辑:程序博客网 时间:2024/04/28 07:30

                                           Flow Problem

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


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
23 21 2 12 3 13 31 2 12 3 11 3 1
 

Sample Output
Case 1: 1Case 2: 2
 
最大流开通的第二题,模板只要稍微改动一下就AC了,一遍AC,1——n的最大流问题,这里是t组测试数据,不断的输入即可。
注释写的很明白,不再在此赘述。
代码如下:
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#define inf 88888888using namespace std;int cap[200][200];int flow[200][200];int a[200];int main(){    int n,m;    int i,j;    int si,ei,ci;    int t;    int k=1;    while(scanf("%d",&t)!=EOF)        //while(scanf("%d %d",&n,&m)!=EOF)    {        while(t--)        {            scanf("%d %d",&n,&m);            memset(cap,0,sizeof(cap));            for(i=0; i<m; i++)            {                scanf("%d %d %d",&si,&ei,&ci);                cap[si][ei]+=ci;            }            queue<int>q;            memset(flow,0,sizeof(flow));            int f=0;            int p[210];            while(1)            {                memset(a,0,sizeof(a));                a[1]=inf;                q.push(1);                while(!q.empty())//BFS找增光链                {                    int u=q.front();                    q.pop();                    for(int v=1; v<=n; v++)                    {                        if(!a[v]&&cap[u][v]>flow[u][v])//找到新节点v                        {                            p[v]=u;//记录v的父亲                            q.push(v);//并加入队列                            a[v] = a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];                            //a[v]=a[u]<?cap[u][v]-flow[u][v];//s-v路径上的最小残量                        }                    }                }                if(a[n]==0) break;//找不到,则当前流已经是最大流                for(int u=n; u!=1; u=p[u])//从汇点往回走                {                    flow[p[u]][u]+=a[n];//更新正向流量                    flow[u][p[u]]-=a[n];//更新反向流量                }                f+=a[n];//更新从s流出的总流量            }            printf("Case %d: %d\n",k++,f);//最后打印总的流量        }    }    return 0;}

 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 kindle屏幕碎了怎么办 kindle内存太小怎么办 百度云解压和谐怎么办 电脑上没有pdf怎么办 word转txt乱码怎么办 双珠卷发器怎么办用 pdf打印出来很小怎么办 热敏打印机无法打印怎么办 pdf格式不能打印怎么办 pdf文件没有打印怎么办 打印机添加不上怎么办 笔记本电脑收不到wifi怎么办 账套输出不了怎么办 无法加载pdf文档怎么办 pdf控件不可用怎么办 扫描件不清晰怎么办 图片大于200k怎么办 手机图片字模糊怎么办 天然气手册丢了怎么办 消消乐登录异常怎么办 新手想学考古怎么办 省份证改名字怎么办 文曲星放太久开不了机怎么办 小狗吃火腿肠皮怎么办 虚火引起的牙痛怎么办 牙髓炎怎么办立刻止疼 小蜜丸吃不下去怎么办 铜钱的字不认识怎么办 古钱币出手好烦怎么办 安装目录不可写怎么办 手机不支持exfat格式怎么办 windows7图标变大了怎么办 igs格式烂曲面怎么办 手机桌面文件夹打不开怎么办 苹果下载不了150怎么办 iphone6速度变慢怎么办 苹果手机微信打不开pdf怎么办 苹果手机打不开pdf怎么办 pdf文件超过了怎么办 pdf电脑删不了怎么办 联想笔记本摄像头横屏调竖屏怎么办