图论(网络流):UVa 1659 - Help Little Laura
Laura Luo has just invented a game. Given a beautiful pencil sketch with n points, you're to colorize it with water pens by painting circuits. Each time you paint a new circuit, starts with one point, follow some line segments and return to the starting point. Every point can be reached more than once, but every segment can be painted at most once. To make the picture look interesting, different segments must be painted different colors. For each segment, Laura has already decided a direction to paint it. The picture below illustrates a possible way to paint the picture (dashed lines are segments that are not painted).

After you finish painting, your score is computed as follows: for each unit length you paint, you earn x points, for each color you use, you lost y points (Laura has prepared enough water pens of different colors).
Write a program to find the maximal score you can get.
Input
The input contains several test cases. The first line of each case contains three positive integers n, x, y (1
n
100, 1
x, y
1000) . The next n lines each describe a point (points are numbered from 1 to n in the order they appear in the input). The first two integers (x, y) specify its coordinates (0
x, y
1000) . The rest integers are the points it connects to, ended by a zero. If point v appears in the list of point u , there is a line segment connecting u and v (then there will not a segment connecting u and v in the reverse direction). Furthermore, Laura will paint it from u to v . There will be no duplicated points and no more than 500 segments. The last test case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and the maximal score you can get, to two decimal places.
Sample Input
4 5 1
0 0 2 3 0
1 0 3 4 0
1 1 4 0
0 1 1 0
1 2 1
0 0 0
10 7 2
0 0 2 4 0
5 0 3 0
5 10 4 10 0
2 3 5 0
7 5 6 0
0 11 1 0
8 0 10 5 0
18 3 7 0
14 5 8 1 0
12 9 9 0
0
Sample Output
Case 1: 16.00
Case 2: 0.00
Case 3: 522.18
这道题就是最小费用循环流的模板,先上代码。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int N=;
const int M=;
const int INF=;
const double dINF=1e9;
const double eps=1e-;
int w[N],cnt,fir[N],to[M],nxt[M];
int cap[M],path[N],vis[N];
double val[M],dis[N];
queue<int>q;
struct Net_Flow{
void Init(){memset(fir,,sizeof(fir));cnt=;} void add(int a,int b,int c,double v){
nxt[++cnt]=fir[a];cap[cnt]=c;
to[cnt]=b;val[cnt]=v;fir[a]=cnt;
} void addedge(int a,int b,int c,double v){
add(a,b,c,v);add(b,a,,-v);
} double Spfa(int S,int T){
fill(dis,dis+T+,dINF);
q.push(S);vis[S]=;dis[S]=;
while(!q.empty()){
int x=q.front();q.pop();vis[x]=;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&dis[to[i]]-dis[x]-val[i]>eps){
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]])q.push(to[i]);
vis[to[i]]=;path[to[i]]=i;
}
}
return dis[T];
} int Aug(int S,int T){
int p=T,f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} double MCMF(int S,int T){
double v=,d;
while((d=Spfa(S,T))!=dINF)
v+=d*Aug(S,T);
return v;
}
}mcmf;
int deg[N];
int a[N],b[N],G[N][N];
int sqr(int x){
return x*x;
}
int main(){
int n,x,y,cas=;double ans;
while(scanf("%d%d%d",&n,&x,&y)!=EOF){
if(!n)break;
mcmf.Init();ans=0.0;
memset(G,,sizeof(G));
memset(deg,,sizeof(deg));
for(int i=;i<=n;i++){
int t;
scanf("%d%d%d",&a[i],&b[i],&t);
while(t){G[i][t]=;scanf("%d",&t);}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)if(G[i][j]){
double v=y-x*sqrt(sqr(a[i]-a[j])+sqr(b[i]-b[j]));
if(v<){
mcmf.addedge(j,i,,-v);
ans+=v;deg[j]+=;deg[i]-=;
}
if(v>)mcmf.addedge(i,j,,v);
} for(int i=;i<=n;i++){
if(deg[i]>)mcmf.addedge(,i,deg[i],);
if(deg[i]<)mcmf.addedge(i,n+,-deg[i],);
}
printf("Case %d: %.2f\n",++cas,eps-ans-mcmf.MCMF(,n+));
}
return ;
}
具体就如代码,网络流还是一般的网络流,就是建边很神。
最后的eps是为了防止输出-0.00的情况。
图论(网络流):UVa 1659 - Help Little Laura的更多相关文章
- UVA 1659 Help Little Laura 帮助小劳拉 (最小费用流,最小循环流)
(同时也是HDU 2982,UVA的数据多) 题意:平面上有m条有向线段连接了n个点.你从某个点出发顺着有向线段行走,给走过的每条线段涂一种不同的颜色,最后回到起点.你可以多次行走,给多个回路涂色(要 ...
- 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)
题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...
- 【uva 1515】Pool construction(图论--网络流最小割 模型题)
题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...
- 【uva 1349】Optimal Bus Route Design(图论--网络流 二分图的最小权完美匹配)
题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继 ...
- 【uva 1658】Admiral(图论--网络流 最小费用最大流)
题意:有个N个点M个边的有向加权图,求1~N的两条不相交路径(除了起点和终点外没有公共点),使得权和最小. 解法:不相交?也就是一个点只能经过一次,也就是我后面博文会讲的"结点容量问题&qu ...
- 【uva 753】A Plug for UNIX(图论--网络流最大流 Dinic)
题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸
410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:pvz.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] Plants vs ...
- 图论--网络流--最大流 洛谷P4722(hlpp)
题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...
随机推荐
- css 权威指南笔记( 五)结构和层叠之三种样式来源
CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式 首先,创作人员(author's+style)样式应该是我们最熟悉的,如果你是一个前端开发者,那么你写的那些样 ...
- Android 布局
转自:http://www.cnblogs.com/chiao/archive/2011/08/24/2152435.html Android布局是应用界面开发的重要一环,在Android中,共有五种 ...
- SQL Server中的20个系统变量
1.@@CONNECTIONS返回自上次启动 Microsoft SQL Server以来连接或试图连接的次数.示例:下面的示例显示了到当前日期和时间为止试图登录的次数.SELECT GETDATE( ...
- URAL 2032 - Conspiracy Theory and Rebranding【本源勾股数组】
[题意] 给出三角形的三个边长,均是10^7以内的整数,问三角形的三个角的坐标是否能均是整数,输出其中任意一个解. [题解] 一开始想的是枚举一条边的横坐标,然后通过勾股定理以及算角度求出其他点的坐标 ...
- 解决mybatis使用枚举的转换
解决mybatis使用枚举的转换 >>>>>>>>>>>>>>>>>>>>> ...
- float与double剖析
今天研究下float与double的编码 float: 我们来看一下这组数是如何一步步从16进制转换到float的 float编码格式: 1.将16进制转换到2进制 整理后:0 1000 0010 1 ...
- ajax xmlhttp下open方法POST、GET参数的区别
1. get是从服务器上获取数据(会暴露客户端ip),post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看 ...
- JSP技术
1. JSP技术简介 JSP全称是Java Server Pages,它和servlet技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.是sun公司定义的一种规范,JSP实际上就是Se ...
- UITapGestureRecognizer会屏蔽掉Button的点击事件( 转载)
UITapGestureRecognis 前几天在做项目的时候,遇到这个一个问题,在一个视图也就是UIView上添加一个手势,然后又在这个View上添加一个UIButton,然后给按钮添加事件,运行项 ...
- JS特殊符号
反斜杠用来在文本字符串中插入省略号.换行符.引号和其他特殊字符. 代码 输出 \' 单引号 \" 双引号 \& 和号 \\ 反斜杠 \n 换行符 \r 回车符 \t 制表符 \b 退 ...