isap测模板--HDU3549

来源:互联网 发布:网络发帖管理规定 编辑:程序博客网 时间:2024/05/16 00:57

Flow Problem

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


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

Sample Output
Case 1: 1
Case 2: 2
/***************************************** author:crazy_石头* algorithm:sap+当前弧+gap优化* date:2013/09/29*****************************************/#include<cstdio>#include<cstring>#include<climits>using namespace std;#define N 90005#define M 4000005#define INF INT_MAXint s,t,num,pre[N],dis[N],gap[N],head[N],cur[N];struct node{     int u,v,w;     int next;}e[M];inline void add(int u,int v,int w){     e[num].u=u;     e[num].v=v;     e[num].w=w;     e[num].next=head[u];     head[u]=num++;}inline void addedge(int u,int v,int w){     add(u,v,w);     add(v,u,0);}inline int max(int a,int b){    return a>b?a:b;}inline int min(int a,int b){    return a<b?a:b;}int isap(int s,int t,int n){     int top,mindis,maxflow=0,v,i,aug;     bool flag;     for(i=0;i<=n;i++)     {         cur[i]=head[i];         gap[i]=dis[i]=0;     }     top=pre[s]=s;     aug=INF;     while(dis[s]<n)     {         flag=0;         for(i=cur[top];i!=-1;i=e[i].next)         {             v=e[i].v;             if(e[i].w>0&&dis[top]==dis[v]+1)             {                 flag=1;                break;             }         }         if(flag)         {             pre[v]=top;             cur[top]=i;             aug=min(aug,e[i].w);             top=v;             if(top==t)             {                 while(top!=s)                 {                     top=pre[top];                     e[cur[top]].w-=aug;                     e[cur[top]^1].w+=aug;                 }                 top=s;                 maxflow+=aug;                 aug=INF;             }         }         else         {             if(--gap[dis[top]]==0)                break;              mindis=n;             cur[top]=head[top];             for(i=head[top];i!=-1;i=e[i].next)             {                 v=e[i].v;                 if(e[i].w>0&&dis[v]+1<mindis)                 {                     mindis=dis[v]+1;                     cur[top]=i;                 }             }             dis[top]=mindis;             gap[mindis]++;             if(top!=s)                 top=pre[top];         }     }     return maxflow;}void init(){     num=0;     memset(head,-1,sizeof(head));}int main(){     int nodenum,m,n,test;     scanf("%d",&test);     for(int ii=1;ii<=test;ii++)     {         scanf("%d%d",&n,&m);         init();         while(m--)         {             int u,v,w;             scanf("%d%d%d",&u,&v,&w);             addedge(u,v,w);         }        //source=1,sink=n,nodenum=n;         int res=isap(1,n,n);         printf("Case %d: %d\n",ii,res);     }     return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 对待孩子脾气暴燥怎么办 买的新鞋子臭脚怎么办 鞋子臭脚怎么办如何除 惠普803墨盒干了怎么办 酷派手机无命令怎么办 华为手机锁机了怎么办 小孩被蜘蛛咬了怎么办 我是一个不爱说话的人怎么办 光动能手表停了怎么办 电波表收不到波怎么办 吃了壮阳药头疼怎么办 吃了伟哥后头痛怎么办 伟哥吃了不管用怎么办 钱站的钱还不起怎么办 面试工资要低了怎么办 线切割环保来查怎么办 喝白酒咽不下去怎么办 翠佛堂的玉假了怎么办 300英雄账号忘了怎么办 买了假酒不承认怎么办 买到假酒不承认怎么办 喝假酒胃里难受怎么办 怀孕初期塞药了怎么办 促黄体生成素高怎么办 激素脸痒的厉害怎么办 肾阴虚又肾阳虚怎么办 泡菜坛子太酸了怎么办 吃花粉过敏休刻怎么办 脸过敏怎么办才最有效 喝花粉后过敏了怎么办 阿比特龙耐药后怎么办 马蜂把人蜇了怎么办 非那雄胺副作用怎么办 来电充电宝丢了怎么办 蜂哈哈过量伤子怎么办 八个月宝宝伤食怎么办 床有虫子咬木板怎么办 床的木板有蛀虫怎么办 中蜂发生盗蜂了怎么办 中蜂发生盗蜂怎么办 孕妇给黄蜂蛰了怎么办