package moduloVII;
import java.io.*;
import java.util.StringTokenizer;
public class M {
static final int WHITE = 0, GRAY = 1;
static double[][] flow;
static int[][] capacity;
static double[][] res_capacity;
static int[] parent, color, queue;
static double[] min_capacity;
static int first, last;
public static void main(String args[]) {
String frase;
while ( ((frase=readLn(20))!=null) ) {
StringTokenizer linha= new StringTokenizer(frase.trim());
int n=Integer.parseInt(linha.nextToken()); //Num de vertices
int m=Integer.parseInt(linha.nextToken()); //Num de liga僖es
capacity=new int[n+1][n+1];
//Vertice de Origem | Vertice de Destino | Peso da liga頒o
for(int i=0; i<m; i++){
StringTokenizer le= new StringTokenizer(readLn(50).trim());
int origem= Integer.parseInt(le.nextToken());
int destino= Integer.parseInt(le.nextToken());
int pesoLigacao=Integer.parseInt(le.nextToken());
capacity[origem][destino] = pesoLigacao;
}
//Argumentos: origem, destino, tamanho
int maxFlow=EdmondsKarp(1, n, n+1);
System.out.println(maxFlow);
}
}
public static int EdmondsKarp(int origem, int destino, int tam){
int max_flow=0;
flow = new double[tam][tam];
res_capacity = new double[tam][tam];
parent = new int[tam];
min_capacity = new double[tam];
color = new int[tam];
queue = new int[tam];
for (int i = 0; i < tam; i++)
for (int j = 0; j < tam; j++)
res_capacity[i][j] = capacity[i][j];
while (BFS(origem, destino, tam)){
max_flow += min_capacity[destino];
int v = destino, u;
while (v != origem){
u = parent[v];
flow[u][v] += min_capacity[destino];
flow[v][u] -= min_capacity[destino];
res_capacity[u][v] -= min_capacity[destino];
res_capacity[v][u] += min_capacity[destino];
v = u;
}
}
return max_flow;
}
// Breadth First Search
public static boolean BFS(int origem, int destino, int tam){
for (int i = 0; i < tam; i++){
color[i] = WHITE;
min_capacity[i] = Double.MAX_VALUE;
}
first = last = 0;
queue[last++] = origem;
color[origem] = GRAY;
while (first != last){ // While "queue" not empty..
int v = queue[first++];
for (int u = 0; u < tam; u++)
if (color[u] == WHITE && res_capacity[v][u] > 0){
min_capacity[u] = Math.min(min_capacity[v], res_capacity[v][u]);
parent[u] = v;
color[u] = GRAY;
if (u == destino)
return true;
queue[last++] = u;
}
}
return false;
}
static String readLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try {
while (lg < maxLg){
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e){
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Implementation of Edmonds Karp algorithm that calculates maxFlow of graph. Input: For each test case, the first line contains the number of vertices (n) and the number of arcs (m). Then, there exist m lines, one for each arc (source vertex, ending vertex and arc weight, separated by a space). The nodes are numbered from 1 to n. The node 1 and node n should be in different sets. There are no more than 30 arcs and 15 nodes. The arc weights vary between 1 and 1 000 000. Output: The output is a single line for each case, with the corresponding minimum size cut. Example: Input: 7 11 1 2 3 1 4 3 2 3 4 3 1 3 3 4 1 3 5 2 4 6 6 4 5 2 5 2 1 5 7 1 6 7 9 Output: 5
资源推荐
资源详情
资源评论
收起资源包目录
M.rar (1个子文件)
M.java 3KB
共 1 条
- 1
资源评论
刘良运
- 粉丝: 77
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- ssoPlusFrontdsfdsfdsfsadawsdad
- Hierarchical Consensus Hashing for Cross-Modal Retrieval
- 基于 C++ OpenCV视觉库实现的计算机视觉分析,得到手掌上五根手指的长度与宽度、手掌虎口的角度、手掌的宽度以及手腕的宽度 完成对手掌各个参数的精确测量课程设计(源码+报告)
- 联想7400打印机更换定影组件.jpg
- 基于servlet+jsp+mysql实现的影视管理系统课程设计
- 正点原子RK3568卡片电脑ATOMPI-CA1的ubuntu-22.04.5最小安装包,特别适合运行板级ROS2环境iron
- GUIdemo.zip
- Ajax应用程序安全(SecuringAjaxApplicationsEnsuringtheSafetyoftheDynamicWeb)p最新版本
- 基于python sqlite和tk库实现的图形化展示的民航管理系统【数据库课程设计】
- 正点原子RK3568卡片电脑ATOMPI-CA1的ubuntu-24.04.1最小安装包,特别适合运行板级ROS2环境jazzy
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功