hihocoder 1369: 网络流一·Ford-Fulkerson算法
来源:互联网 发布:超级sql注入工具教程 编辑:程序博客网 时间:2024/05/22 08:14
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
- 样例输入
6 71 2 31 3 52 4 13 4 23 5 34 6 45 6 2
最大流算法:
import java.util.Arrays;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scan=new Scanner(System.in);int num=scan.nextInt();int[][] map=new int[num][num];int count=scan.nextInt();for(int i=0;i<count;i++){int x=scan.nextInt()-1;int y=scan.nextInt()-1;int val=scan.nextInt();map[x][y]+=val; //map[y][x]+=val;}Main main=new Main();System.out.println(main.maxFlow(map, 0, num-1));}public int maxFlow(int[][] graph,int s,int t){int[][] rGraph=new int[graph.length][graph[0].length];for(int i=0;i<rGraph.length;i++){for(int j=0;j<graph[0].length;j++){rGraph[i][j]=graph[i][j];}}int re=0;int[] path=new int[graph.length];Arrays.fill(path,-1);while(hasPath(rGraph,s,t,path)){int minFlow=Integer.MAX_VALUE;for(int i=t;i!=s;i=path[i]){minFlow=Math.min(minFlow, rGraph[path[i]][i]);}for(int i=t;i!=s;i=path[i]){rGraph[path[i]][i]-=minFlow;//rGraph[i][path[i]]+=minFlow;}re+=minFlow;Arrays.fill(path,-1);}return re;}public boolean hasPath(int[][] graph,int s,int t,int[] path){boolean[] visited=new boolean[graph.length];Queue<Integer> queue=new LinkedList<Integer>();queue.add(s);visited[s]=true;while(!queue.isEmpty()){int top=queue.poll();for(int i=0;i<graph[0].length;i++){if(visited[i]) continue;if(graph[top][i]>0){queue.add(i);visited[i]=true;path[i]=top;//把路径存储到path中}}}return visited[t];}}
描述
小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个大城市都会遇到的问题:交通拥挤。
小Ho:每到周末回家感觉堵车都是一种煎熬啊。
小Hi:平时交通也还好,只是一到上下班的高峰期就会比较拥挤。
小Ho:要是能够限制一下车的数量就好了,不知道有没有办法可以知道交通系统的最大承受车流量,这样就可以限制到一个可以一直很顺畅的数量了。
小Hi:理论上是有算法的啦。早在1955年,T.E.哈里斯就提出在一个给定的网络上寻求两点间最大运输量的问题。并且由此产生了一个新的图论模型:网络流。
小Ho:那具体是啥?
小Hi:用数学的语言描述就是给定一个有向图G=(V,E),其中每一条边(u,v)均有一个非负数的容量值,记为c(u,v)≥0。同时在图中有两个特殊的顶点,源点S和汇点T。
举个例子:
其中节点1为源点S,节点6为汇点T。
我们要求从源点S到汇点T的最大可行流量,这个问题也被称为最大流问题。
在这个例子中最大流量为5,分别为:1→2→4→6,流量为1;1→3→4→6,流量为2;1→3→5→6,流量为2。
小Ho:看上去好像挺有意思的,你让我先想想。
提示:Ford-Fulkerson算法
输入
第1行:2个正整数N,M。2≤N≤500,1≤M≤20,000。
第2..M+1行:每行3个整数u,v,c(u,v),表示一条边(u,v)及其容量c(u,v)。1≤u,v≤N,0≤c(u,v)≤100。
给定的图中默认源点为1,汇点为N。可能有重复的边。
输出
第1行:1个整数,表示给定图G的最大流。
阅读全文
0 0
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder 1369: 网络流一·Ford-Fulkerson算法
- hihocoder#1369 : 网络流一·Ford-Fulkerson算法
- hihoCoder 题目1 : 网络流一·Ford-Fulkerson算法
- 网络流一·Ford-Fulkerson算法
- hihoCoder 1369 网络流一·Ford-Fulkerson算法 (网络流学习#1 记录)
- hihoCoder-第115周-网络流一·Ford-Fulkerson算法
- hihocode#1369 : 网络流一·Ford-Fulkerson算法(EK)
- hihocoder1369 网络流一·Ford-Fulkerson算法
- 网络流--ford-fulkerson算法
- 网络流Ford—Fulkerson算法
- 网络流问题Ford-Fulkerson算法详解
- 最大网络流的Ford-Fulkerson 算法
- 增广路算法 Ford-Fulkerson算法 网络最大流问题
- 网络流Ford-fulkerson算法及dinic算法
- 网络流 (Ford-Fulkerson)
- 最大流 Ford-Fulkerson算法
- 之字形二叉树
- c++ 中 pair 的 使用方法
- BZOJ 2565 Manacher 解题报告
- LSTM 网络,易理解
- struts2(培训笔记)
- hihocoder 1369: 网络流一·Ford-Fulkerson算法
- mac下xampp+thinkphp运行htdocs文件夹下所建项目index.php报错的解决方案
- 手机访问电脑本地服务器
- Django学习之路(八)Admin的学习
- BUGKU web--writeup
- 多线程join daemon
- 前端框架这么多,该何去何从?
- node.js中get与post请求的处理
- 用css画三角形并附加评分