图论(网络流):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,用空格分隔,分别表示点的个数.有向边的个数.源 ...
随机推荐
- UICollectionView reloadData后cell被隐藏
在使用UICollectionView的页面执行: [self.collectionView reloadData]; 执行后,页面变为空白页,调试发现,执行reloadData 后UICollect ...
- Python之路【第十篇】:HTML -暂无等待更新
Python之路[第十篇]:HTML -暂无等待更新
- codevs1024一塔湖图(丧心病狂的建图)
/* 丧心病狂的最短路 关键是建图 根据题目中给的路 拆出节点来 建图 (i,j) -->(j-1)*n+i 然后根据障碍 把死路 湖覆盖的dis改变成极大值 然后Floyd 然后 然后就没有然 ...
- C# 二叉查找树实现
BuildTree 代码1次CODE完,没有BUG. 在画图地方debug了很多次.第一次画这种图. 一开始用treeview显示,但发现不是很好看出树结构,于是自己动手画了出来. using Sys ...
- Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务
1.BootCompletedReceiver.Java文件 public class BootCompletedReceiver extends BroadcastReceiver { @Overr ...
- 第四篇、Tomcat 集群
1. 前言 该篇中测试的机器发生了变更,在第一篇中设置的Apache DocumentRoot "d:/deployment"修改为了DocumentRoot d:/clust ...
- window对象细节(转载)
Window对象是客户端javascript最高层对象之一,只要打开浏览器窗口,不管该窗口中是否有打开的网页,当遇到BODY.FRAMESET或FRAME元素时,都会自动建立window对象的实例.另 ...
- seaJs初体验
目录结构 模块定义define define(function(require,exports,module){ //exports可以把方法或属性暴露给外部 exports.name = 'hell ...
- 单选按钮 点击value值自动把单选按钮选中
HTML 代码 <tr> <td align="right">性别:</td> <td><inputt ...
- JS自执行函数的几种写法
一:整体写在一个括号中 代码如下: (function Show(){alert("hello");}()) 二:function函数整体外加括号 代码如下: (function ...