HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1181 Accepted Submission(s): 473
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[N];
bool vis[N];
int dist[N];
int iter[N];
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear();
es.clear();
}
void addedge(int from,int to,int cap)
{
es.push_back((edge)
{
from,to,cap,
});
es.push_back((edge)
{
to,from,,
});
int x=es.size();
G[from].push_back(x-);
G[to].push_back(x-);
}
bool BFS(int s,int t)
{
memset(vis,false,sizeof(vis));
queue <int> Q;
vis[s]=true;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for (int i=; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
dist[e.to]=dist[u]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=,d;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if(dist[u]+==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>)
{
e.flow+=d;
es[G[u][i]^].flow-=d;
flow+=d;
f-=d;
if(f==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=;
while(BFS(s,t))
{
memset(iter,,sizeof(iter));
int d=;
while(d=DFS(s,t,inf)) flow+=d;
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,s,t;
scanf("%d%d",&n,&m);
scanf("%d%d",&s,&t);
for(int i=; i<=m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
Maxflow(s,t);
for(int i=; i<es.size(); i+=)
{
if(es[i].flow==es[i].cap) es[i].cap++;
else es[i].cap=inf;
}
printf("%d\n",Maxflow(s,t));
init(n);
}
return ;
}
方案一
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[N];
bool vis[N];
int dist[N];
int iter[N];
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear();
es.clear();
}
void addedge(int from,int to,int cap)
{
es.push_back((edge)
{
from,to,cap,
});
es.push_back((edge)
{
to,from,,
});
int x=es.size();
G[from].push_back(x-);
G[to].push_back(x-);
}
bool BFS(int s,int t)
{
memset(vis,false,sizeof(vis));
queue <int> Q;
vis[s]=true;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for (int i=; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
dist[e.to]=dist[u]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=,d;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if(dist[u]+==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>)
{
e.flow+=d;
es[G[u][i]^].flow-=d;
flow+=d;
f-=d;
if(f==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=;
while(BFS(s,t))
{
memset(iter,,sizeof(iter));
int d=;
while(d=DFS(s,t,inf)) flow+=d;
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,s,t;
scanf("%d%d",&n,&m);
scanf("%d%d",&s,&t);
for(int i=; i<=m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w*(m+)+);
}
printf("%d\n",Maxflow(s,t)%(m+));
init(n);
}
return ;
}
方案二
HDU 6214.Smallest Minimum Cut 最少边数最小割的更多相关文章
- HDU 6214 Smallest Minimum Cut(最少边最小割)
Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...
- hdu 6214 Smallest Minimum Cut[最大流]
hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...
- hdu 6214 Smallest Minimum Cut(最小割的最少边数)
题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...
- HDU 6214 Smallest Minimum Cut (最小割且边数最少)
题意:给定上一个有向图,求 s - t 的最小割且边数最少. 析:设边的容量是w,边数为m,只要把每边打容量变成 w * (m+1) + 1,然后跑一个最大流,最大流%(m+1),就是答案. 代码如下 ...
- HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】
Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...
- hdu 6214 : Smallest Minimum Cut 【网络流】
题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...
- Smallest Minimum Cut HDU - 6214(最小割集)
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU-6214 Smallest Minimum Cut(最少边最小割)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 Problem Description Consider a network G=(V,E) w ...
随机推荐
- win10+vs2015编译caffe的cpu debug版本、部署matcaffe
一.编译caffe 1.安装python-3.5.2-amd64.exe https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe ...
- 【java】static用法
static作用: 用来修饰函数成员,成员变量和成员函数.类对象的属性都一致且能共享,比如国籍,这就能用static修饰,name不能共享,因为每个人都有自己的名字. 特有内容(name)随着对象存储 ...
- 过滤器手动注入Service Bean方法
@Override public void init(FilterConfig arg0) throws ServletException { ServletContext servletContex ...
- MFC文件处理
计算机室如何管理自身所存放着的大量的信息的呢?windows的磁盘管理程序为我们提供了一套严密而又高效的信息组织形式--硬盘上的信息是以文件的形式被管理的. 面向存储的文件技术 什么是文件?计算机中, ...
- 几种流行Webservice框架
一. 几个比较流行的Webservice框架: Apache Axis1.Apache Axis2.Codehaus XFire.Apache CXF.Apache Wink.Jboss RESTE ...
- 1、ZooKeeper 基本概念、使用方法、实践场景
ZooKeeper 基本概念 ZooKeeper 是面向分布式应用的协调服务,其实现了树形结构的数据模型(与文件系统类似),并且提供了简洁的编程原语.ZooKeeper 能够作为基础,用于构建更高层级 ...
- Struts2 环境搭建
1.引入相关struts2 jar包 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <we ...
- Hibernate Criteria使用
hibernate中Criteria的完整用法 Criteria 是一个完全面向对象,可扩展的条件查询API,通过它完全不需要考虑数据库底层如何实现.SQL语句如何编写,是Hibernate框架的核心 ...
- django 补充和中间件
配置 from django.conf import settings form组件 from django.forms import Formfrom django.forms import fie ...
- requests_html 使用
安装 pip install requests-html #2种方式爬取 博客园from requests_html import HTMLSession session=HTMLSession() ...