图论(网络流):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 (1n
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,用空格分隔,分别表示点的个数.有向边的个数.源 ...
随机推荐
- Bash函数使用
#!/bin/bash function Fun_Name() { #function here echo "this is a function" } Fun_Name
- Maven学习总结——聚合与继承
一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模块一</module&g ...
- 2进制,16进制,BCD,ascii,序列化对象相互转换
public final static char[] BToA = "0123456789abcdef".toCharArray() ; 1.16进制字符串转为字节数组 /** * ...
- 在java中使用 File.renameTo(File)实现重命名.
Here is part of my files: [北京圣思园Java培训教学视频]Java.SE.前9日学习成果测试题(2010年12月2日).rar [北京圣思园Java培训教学视频]Java. ...
- ASP.NET mvc 遇见的问题
1.数据库配置 The specified named connection is either not found in the configuration, not intended to be ...
- Asp与Asp.Net的区别
今天在网上看到一位朋友问asp与asp.net的区别.编辑本人也是从asp转型到.net来的,几年了,几乎都忘记了asp的存在,也说不出它们之间的区别,因为感觉两者是根本就没有联系,非要说有联系,那就 ...
- hdoj 2057
代码: #include <stdio.h>#include<string.h>int main(){ __int64 a,b,c; while(scanf("%I6 ...
- 封装Timer
System.Timers.Timer,System.Timers.Timer在使用的过程中需要: 1.构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.T ...
- swfupload上传文件问题
如果你的框架用到了struts2的话 可能会造成request冲突 那么解决的办法就是把该request排除出去 不让struts2拦截
- 阿里云 centos vim 显示中文 乱码
开始以为是vim 设置编码的问题 :网上搜 改 .vimrc 无效!!! 后转战 是不是系统里面没有中文字体 1.先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的 ...