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 (1n100, 1x, y1000) . 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 (0x, y1000) . 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的更多相关文章

  1. UVA 1659 Help Little Laura 帮助小劳拉 (最小费用流,最小循环流)

    (同时也是HDU 2982,UVA的数据多) 题意:平面上有m条有向线段连接了n个点.你从某个点出发顺着有向线段行走,给走过的每条线段涂一种不同的颜色,最后回到起点.你可以多次行走,给多个回路涂色(要 ...

  2. 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)

    题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...

  3. 【uva 1515】Pool construction(图论--网络流最小割 模型题)

    题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...

  4. 【uva 1349】Optimal Bus Route Design(图论--网络流 二分图的最小权完美匹配)

    题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继 ...

  5. 【uva 1658】Admiral(图论--网络流 最小费用最大流)

    题意:有个N个点M个边的有向加权图,求1~N的两条不相交路径(除了起点和终点外没有公共点),使得权和最小. 解法:不相交?也就是一个点只能经过一次,也就是我后面博文会讲的"结点容量问题&qu ...

  6. 【uva 753】A Plug for UNIX(图论--网络流最大流 Dinic)

    题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...

  7. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  8. 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸

    410. [NOI2009] 植物大战僵尸 ★★★   输入文件:pvz.in   输出文件:pvz.out   简单对比时间限制:2 s   内存限制:512 MB [问题描述] Plants vs ...

  9. 图论--网络流--最大流 洛谷P4722(hlpp)

    题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...

随机推荐

  1. Configuring Robolectric

    There are numerous ways to customize how Robolectric behaves at runtime. Config Annotation The prima ...

  2. Java-Android 之电话拨号源码

    file:///F:/workspace3/Android_ver2.4/src/cn/szy/com/MainActivity.java package cn.szy.com; import and ...

  3. 把Excel数据导入到数据库

    引入命名空间 using System.IO; using System.Data; using System.Data.OleDb; 引入命名空间 首先要把Excel上传到服务器 //上传Excel ...

  4. angularjs google map markers+ ui-gmap-windows --->增加click 事件

    jsp: <div class="modal-body viewOnMap"> <div class="cboxClose" ng-click ...

  5. WCF存储图片到指定文件夹下

    string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...

  6. Java封装的与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前

    public class CalendarCal { /**  * 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前  *   * @param calendar  *       ...

  7. 【转载】一步一步搭建自己的iOS网络请求库

    一步一步搭建自己的iOS网络请求库(一) 大家好,我是LastDay,很久没有写博客了,这周会分享一个的HTTP请求库的编写经验. 简单的介绍 介绍一下,NSURLSession是iOS7中新的网络接 ...

  8. C# XML文件操作类XmlHelper

    类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...

  9. VisualStudio2013&VS2015内置SQLServer入门 (三)

    关于LocalDB的部署(publish): 使用本机做服务器(目测不可行) 双击项目的Properties-->Publish-->Application Files,你会发现没有.md ...

  10. CSS3美化表单控件

    表单的默认控件在不同的浏览器中的样式不同,用户体验很差.用CSS3可以实现表单控件的美化,可以提供更好的用户体验.不足之处就是浏览器的兼容性问题. 一.下拉控件 效果图: 下拉控件的布局结构: < ...