(Dinic) hdu 3549

Flow Problem

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

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

HyperHexagon

Source

HyperHexagon‘s Summer Gift (Original tasks)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x7fffffff
queue<int> q;
int tab[250][250];
int dis[250];
int N,M,ANS;
int BFS()
{
     memset(dis,-1,sizeof(dis));
     dis[1]=0;
     q.push(1);
     while (!q.empty())
     {
           int x=q.front();
           q.pop();
           for (int i=1;i<=N;i++)
               if (dis[i]<0&&tab[x][i]>0)
               {
                  dis[i]=dis[x]+1;
                  q.push(i);
               }
     }
     if(dis[N]>0)
        return 1;
     else
        return 0;
}
int find(int x,int low)
{
    int a=0;
    if (x==N)return low;
    for (int i=1;i<=N;i++)
    if (tab[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,tab[x][i]))))
    {
       tab[x][i]-=a;
       tab[i][x]+=a;
       return a;
    }
    return 0;
}
int main()
{
    int tt,f,t,flow,tans,cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
            scanf("%d%d",&N,&M);
            memset(tab,0,sizeof(tab));
            for(int i=1;i<=M;i++)
            {
                  scanf("%d%d%d",&f,&t,&flow);
                  tab[f][t]+=flow;
            }
            ANS=0;
            while(BFS())
            {
                  while(tans=find(1,INF))ANS+=tans;
            }
            printf("Case %d: %d\n",cas,ANS);
            cas++;
    }
    return 0;
}

  

时间: 2024-10-12 15:14:04

(Dinic) hdu 3549的相关文章

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

带权并查集(含种类并查集)【经典模板】 例题:①POJ 1182 食物链(经典)②HDU - 1829 A bug&#39;s life(简单) ③hihoCoder 1515 : 分数调查

带权并查集: 增加一个 value 值,并且每次合并和查找的时候需要去维护这个 value 例题一 :POJ 1182 食物链(经典) 题目链接:https://vjudge.net/contest/339425#problem/E 带权并查集的解法 定义两个数组fa[ ]和rela[ ],fa用来判断集合关系,rela用来描述其与根节点的关系.因为关系满足传递性,所以可以推导出给出条件下的当前关系,在判断与之前已有关系是否矛盾. 本题的解法巧妙地利用了模运算,rela数组用0表示同类,1表示当

(最小费用流)hdu 6118(2017百度之星初赛B 1005) 度度熊的交易计划

度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 260    Accepted Submission(s): 83 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片

18.11.23 POJ 3436 ACM Computer Factory(dinic)

描述 As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory. Every ACM computer consists of P parts. When all the

(贪心)HDU 1789 解题报告

Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7100    Accepted Submission(s): 4209 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he h

博弈论 SG函数(模板) HDU 1848 Fibonacci again and again

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9296    Accepted Submission(s): 3893 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;

网络流模板(Dinic)

1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld;

图论——最大流(DINIC)

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; int dis[300];//BFS深度int q[30000],h,r;//BFS队列 int e;//终点 int head[300];//邻接表头指针 int current[300];//当前弧优化struct

ACM Computer Factory(dinic)

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5596   Accepted: 1922   Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. Th