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 ...
随机推荐
- Batch Close process
@echo offecho.rem Kill all chrome drivertaskkill /im chromedriver.exe /f pause
- <ROS> 机器人描述--URDF和XACRO
文章转自 https://blog.csdn.net/sunbibei/article/details/52297524 特此鸣谢原创作者的辛勤付出 1 URDF 文件 1.1 link和joint ...
- Windows驱动开发
http://blog.csdn.net/sagittarius_warrior/article/details/51000241
- 过滤器手动注入Service Bean方法
@Override public void init(FilterConfig arg0) throws ServletException { ServletContext servletContex ...
- window 10 专业版激活|win 10专业版激活码
下面讲解Windows 10专业版(windows 10 profession version)使用激活码激活 鼠标移至屏幕最左下处右击点击 Windows PowerShell(管理员) 在wind ...
- Java备份文件
文件名后面补时间: public static void initFile(String sPath) { SimpleDateFormat df = new SimpleDateFormat(&qu ...
- css:清楚html所有标签自带属性
相信如果您动手写过网页的话,应该体会到有些标签会自带一些默认的样式,而这些样式或许又是我们不想要的,所以我们可以用以下代码清除所有标签的默认样式 html, body, div, span, ap ...
- git从远程分支clone项目到本地,切换分支命令,其他常用命令
1.在git命令窗口输入git clone git@139.129.217.217:sg/sgsq_cms.git 回车,即可克隆远程项目到本地.红色字体为远程分支的SSHkey,可以登录到gitli ...
- Spring再接触 IOC DI
直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...
- html css col-md-offset
有的时候,我们不想让两个相邻的列挨在一起,这时候利用栅格系统的列偏移(offset)功能来实现,而不必再定义margin值.使用.col-md-offset-*形式的样式就可以将列偏移到右侧.例如,. ...