hdu 3549 Flow Problem 网络流
创始人
2024-02-09 06:24:08
0

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

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.

题意:不能再简单了这题意,很裸的告诉你根据输入来求解最大流。

解法:网络流算法求解最大流,模板题,Dinic。

 1 #include2 #include3 #include4 #include5 #include6 #include7 #include8 #define inf 0x7fffffff9 using namespace std;
10 const int maxn=16;
11 
12 int n,m;
13 int graph[maxn][maxn],d[maxn];
14 
15 int bfs()
16 {
17     memset(d,0,sizeof(d));
18     d[1]=1;
19     queue Q;
20     Q.push(1);
21     while (!Q.empty())
22     {
23         int u=Q.front() ;Q.pop() ;
24         for (int v=1 ;v<=n ;v++)
25         {
26             if (!d[v] && graph[u][v]>0)
27             {
28                 d[v]=d[u]+1;
29                 Q.push(v);
30                 if (v==n) return 1;
31             }
32         }
33     }
34     return 0;
35 }
36 
37 int dfs(int u,int flow)
38 {
39     if (u==n || flow==0) return flow;
40     int cap=flow;
41     for (int v=1 ;v<=n ;v++)
42     {
43         if (d[v]==d[u]+1 && graph[u][v]>0)
44         {
45             int x=dfs(v,min(cap,graph[u][v]));
46             cap -= x;
47             graph[u][v] -= x;
48             graph[v][u] += x;
49             if (cap==0) return flow;
50         }
51     }
52     return flow-cap;
53 }
54 
55 int Dinic()
56 {
57     int sum=0;
58     while (bfs()) sum += dfs(1,inf);
59     return sum;
60 }
61 
62 int main()
63 {
64     int t,ncase=1;
65     scanf("%d",&t);
66     while (t--)
67     {
68         scanf("%d%d",&n,&m);
69         int a,b,c;
70         memset(graph,0,sizeof(graph));
71         for (int i=0 ;i 

相关内容

热门资讯

嵩山少林寺对联 嵩山少林寺对联九州名山数嵩岳四海古刹推少林
哪里可以看关于不知火舞的动漫 哪里可以看关于不知火舞的动漫专门描写不知火舞的我还没有听说,不过部分的话,你去看拳皇把。专门描写KI...
出租车为什么叫TAXI或的士? 出租车为什么叫TAXI或的士?出租车的英文是TAXI。 “的士”应该是广东香港一带的说法,用广东话说...
江雪这首诗的作者是? 江雪这首诗的作者是?《江雪》这首诗的作者是柳宗元《江雪》作者:柳宗元 (唐)千山鸟飞绝,万径人踪灭。...
王昌龄《出塞》里的度阴山 究竟... 王昌龄《出塞》里的度阴山 究竟是哪个度字 是渡还是度度,在文中是翻过昆仑山的意思是“度”字,是“过,...
求推荐几本类似于惊世亡妃,夜行... 求推荐几本类似于惊世亡妃,夜行歌,女子无殇的小说,就是情节跌宕起伏,男主深爱女主,好结局的古代言情求...
找一部小说,都市类型,女主高冷... 找一部小说,都市类型,女主高冷总裁,叫慕什么!男主和女主是没有公开结婚,男主在女主公司做的小保安,是...
往生问题? 往生问题?临终的人才回答得到你。但参与多次助念活动中,那些往生者临终前回光返照,都说已经见到阿弥陀佛...
怎样追求自己喜欢的女孩子 怎样追求自己喜欢的女孩子她是隔壁班的女孩子,非常可爱。我该怎么追她呢?我是学生。送花..然后悄悄表白...
你是不是饿的荒啊,十娘我给你做... 你是不是饿的荒啊,十娘我给你做面汤是什么歌 《杜十娘下面汤》曲调是江苏扬州民歌《杨柳青》杜十娘~...