zoj-3792-Romantic Value-最小割+数值转化
假设不须要求边的个数的话,就是一个裸的最小割问题。
求边的个数就用边的权值记录一下。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include<queue>
using namespace std;
#define INF 99999999
#define LL long long
const LL maxn =55;
const LL maxm =4400;
const LL oo = (LL)1<<37;
struct Arclist
{
LL cnt, head[maxn], dis[maxn];
LL cur[maxn], pre[maxn], gap[maxn], aug[maxn];
struct node
{
LL u, v, w, next;
}edge[maxm];
void init()
{
cnt = 0;
memset(head,-1,sizeof(head));
}
void add(LL u, LL v, LL w)
{
// cout<<u<<" "<<v<<" "<<w<<endl;
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].w = 0;
edge[cnt].next = head[v];
head[v] = cnt++;
}
LL sap(LL s, LL e, LL n)
{
LL max_flow = 0, u = s;
LL mindis;
for(LL i = 0; i <= n; i++)
{
cur[i] = head[i];
dis[i] = 0;
gap[i] = 0;
}
aug[s] = oo;
pre[s] = -1;
gap[0] = n;
while(dis[s]<n)
{
bool flag = false;
if(u==e)
{
max_flow += aug[e];
for(LL v = pre[e]; v != -1; v = pre[v])
{
LL id = cur[v];
edge[id].w -= aug[e];
edge[id^1].w += aug[e];
aug[v] -= aug[e];
if(edge[id].w==0) u = v;
}
}
for(LL id = cur[u]; id != -1; id = edge[id].next)
{
LL v = edge[id].v;
if(edge[id].w>0 && dis[u]==dis[v]+1)
{
flag = true;
pre[v] = u;
cur[u] = id;
aug[v] = std::min(aug[u], edge[id].w);
u = v;
break;
}
}
if(flag==false)
{
if(--gap[dis[u]]==0) break;
mindis = n;
cur[u] = head[u];
for(LL id = head[u]; id != -1; id = edge[id].next)
{
LL v = edge[id].v;
if(edge[id].w>0 && dis[v]<mindis)
{
mindis = dis[v];
cur[u] = id;
}
}
dis[u] = mindis+1;
++gap[dis[u]];
if(u!=s) u = pre[u];
}
}
return max_flow;
}
}G;
int main()
{
LL m,n,u,v,w,T,st,ed;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&n,&m,&st,&ed);
G.init();
LL all=0;
while(m--)
{
scanf("%lld%lld%lld",&u,&v,&w);
G.add(v,u,w*1000+1);
G.add(u,v,w*1000+1);
all+=w;
}
LL w=G.sap(st,ed,n);
// cout<<w<<endl;
if(w==0)cout<<"Inf"<<endl;
else
{
double x,y;
if(w%1000==0)
{
y=1000;
w=w-1000;
}
else y=w%1000;
x=all-w/1000;
printf("%.2f\n",1.0*x/y);
}
}
return 0;
}
zoj-3792-Romantic Value-最小割+数值转化的更多相关文章
- ZOJ 3792 Romantic Value 最小割(最小费用下最小边数)
求最小割及最小花费 把边权c = c*10000+1 然后跑一个最小割,则flow / 10000就是费用 flow%10000就是边数. 且是边数最少的情况.. #include<stdio. ...
- tyvj P1209 - 拦截导弹 平面图最小割&&模型转化
P1209 - 拦截导弹 From admin Normal (OI)总时限:6s 内存限制:128MB 代码长度限制:64KB 背景 Background 实中编程者联盟为了培养技 ...
- ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)
[题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...
- ZOJ 2587 Unique Attack (最小割唯一性)
题意 判断一个无向图的割是否唯一 思路 错误思路:一开始想的是判断割边是否都是关键割边,那既然割边两端点能连通S.T点的边是关键边,那么只要遇到有某个边两端点不连通S or T则这条边就不是关键割边( ...
- zoj 3792 Romantic Value
题目链接 求最小割的值, 以及割边最少的情况的边数. 先求一遍最小割, 然后把所有割边的权值变为1, 其他边变成inf, 在求一遍最小割, 此时求出的就是最少边数. Inf打成inf WA了好几发. ...
- zoj 2587 Unique Attack 最小割判定
题目链接 让你判断最小割是否唯一. 判断方法是, 先求一遍最大流, 然后从源点dfs一次, 搜索未饱和边的数目. 从汇点dfs一次, 同样也是搜索未饱和边的数目, 看总和是否等于n. 如果等于n那么唯 ...
- zoj 2532 Internship【最小割】
就是求哪些边在最大流上满流,也就是找割边.把0作为t点,s向所有的1~n连流量为inf的边,其他的边按照流量连.跑一遍最大流,从s顺着有残余流量的正向边dfs打标记fr,从t顺着正向边有残余流量的反向 ...
- Atcoder Regular Contest 125 E - Snack(最小割转化+贪心)
Preface: 这是生平第一道现场 AC 的 arc E,也生平第一次经历了 performance \(\ge 2800\),甚至还生平第一次被 hb 拉到会议里讲题,讲的就是这个题,然鹅比较尬 ...
- HDU 4289:Control(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...
随机推荐
- windows phone (24) Canvas元素A
原文:windows phone (24) Canvas元素A Canvas元素表示定制一个区域,并可以通过相对坐标定义子元素位置,在一下情况下Canvas是不可见的 Height 属性等于 0. W ...
- Ubuntu下超实用的命令
1. Ubuntu中查看已安装软件包的方法 sudodpkg -l 2. ubuntu系统如何查看软件安装的位置 dpkg-L软件名 实例: wwx@ubuntu:~$dpkg -L mysql-se ...
- (算法入门经典大赛 优先级队列)LA 3135(之前K说明)
A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor da ...
- flex4 一些项目使用的技术
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- html 格式的email 编辑
本篇文章只讲如何编辑html格式的email 模板,并不讲述如何用程序发送email. 1.做email的重要思想:“复古” 抛弃现代化的div+css技术,回到html4.0+table的时代.少用 ...
- MATLAB描绘极坐标图像——polar
polar可用于描绘极坐标图像. 最简单而经常使用的命令格式:POLAR(THETA, RHO) 当中,THETA是用弧度制表示的角度,RHO是相应的半径. 例: a=-2*pi:.001:2*pi ...
- 【从翻译mos文章】不再用par file如果是,export or import 包含大写和小写表名称表
不再用par file如果是,export or import 包含大写和小写表名称表 参考原始: How to Export or Import Case Sensitive Tables With ...
- HDOJ 5188 zhx and contest 贪婪+01背包
zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- ASP.NET自定义控件组件开发 第一章 待续
原文:ASP.NET自定义控件组件开发 第一章 待续 第一章:从一个简单的控件谈起 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接 ...