• hdoj 3549 Flow Problem 【最大流入门 dinic算法】


    Flow Problem

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


     

    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

    第一道最大流,dinic算法:

    #include 
    #include 
    #include 
    #include 
    #include 
    #define MAXN 100+10
    #define MAXM 2000+10
    #define INF 10000000+10
    using namespace std;
    struct Edge
    {
    	int from, to, cap, flow, next;
    }edge[MAXM];
    int n, m;
    int dist[MAXN];//距离起点的距离      
    int top, head[MAXN], vis[MAXN];
    int cur[MAXN];//cur[i]表示i点正在考虑的边 优化不再考虑已经用过的点 初始化为head 
    void init()
    {
    	top = 0;
    	memset(head, -1, sizeof(head));
    }
    void addedge(int a, int b, int c)
    {
    	Edge E1 = {a, b, c, 0, head[a]};
    	edge[top] = E1;
    	head[a] = top++;
    	Edge E2 = {b, a, 0, 0, head[b]};
    	edge[top] = E2;
    	head[b] = top++;
    }
    void getmap()
    {
    	int a, b, c;
    	while(m--)
    	{
    		scanf("%d%d%d", &a, &b, &c);
    		addedge(a, b, c);
    	}
    }
    bool BFS(int start, int end)
    {
    //从残余网络找一条从start->end的路径(就是最短路的代码,每条边的边权为最大流量-当前流量,当这条边满流时最大流量=当前流量,边权为0)   
    //显然当找不到这样的路径时说明不能继续增加流量 则返回false 算法结束   	
    	int i;
    	memset(dist, -1, sizeof(dist));
    	memset(vis, 0, sizeof(vis));
    	queue Q;
    	while(!Q.empty()) Q.pop();
    	Q.push(start);
    	vis[start] = 1;
    	dist[start] = 0;
    	while(!Q.empty())
    	{
    		int u = Q.front();
    		Q.pop();
    		for(i = head[u]; i != -1; i = edge[i].next)
    		{
    			Edge E = edge[i];
    			if(!vis[E.to] && E.cap > E.flow)
    			{
    				vis[E.to] = 1;
    				dist[E.to] = dist[u] + 1;
    				if(E.to == end) return true;
    				Q.push(E.to); 
    			}
    		}
    	}
    	return false;
    }
    int DFS(int x, int a, int end)//增广路径 叠加当前start->end路径上的最小残量 a 
    {
    	if(x == end || a == 0) return a;
    	int flow = 0, f;
    	for(int& i = cur[x]; i != -1; i = edge[i].next)//从上次考虑的弧 
    	{
    		Edge& E = edge[i];
    		if(dist[x]+1 == dist[E.to] && (f = DFS(E.to, min(a, E.cap-E.flow), end)) > 0)
    		{
    			E.flow += f;
    			edge[i^1].flow -= f;
    			flow += f;
    			a -= f;
    			if(a == 0) break;
    		}
    	}
    	return flow;
    }
    int Maxflow(int start, int end)
    {
    	int flow = 0;
    	while(BFS(start, end))//找到1到n的最短路径 
    	{
    		memcpy(cur, head, sizeof(head));
    		flow += DFS(start, INF, end);
    	}
    	return flow;
    }
    int main()
    {
    	int t;
    	int k = 1;
    	scanf("%d", &t);
    	while(t--)
    	{
    		scanf("%d%d", &n, &m);
    		init();
    		getmap();
    		printf("Case %d: %d\n", k++, Maxflow(1, n));
    	}
    	return 0;
    }
  • 相关阅读:
    Python高级语法----Python C扩展与性能优化
    数仓模块ods层—每日行情数据入库(01)
    算法笔记-归并排序
    jq 清除input file的值
    JsonCpp JSON格式处理库的介绍和使用(面向业务编程-文件格式处理)
    LLVM MC layer框架说明
    Windows 打包 Docker 提示环境错误: no DOCKER_HOST environment variable
    7.2 IDA 破解实例
    Abaqus多孔材料、多孔介质、双相材料、随机三维多孔结构建模插件:Random Porous Structure 3D
    数据可视化实战:如何给毛*易的歌曲做词云展示?
  • 原文地址:https://blog.csdn.net/stationinthemind/article/details/127862716