POJ 3155 Hard Life
Hard Life
This problem will be judged on PKU. Original ID: 3155
64-bit integer IO format: %lld Java class name: Main
John is a Chief Executive Officer at a privately owned medium size company. The owner of the company has decided to make his son Scott a manager in the company. John fears that the owner will ultimately give CEO position to Scott if he does well on his new manager position, so he decided to make Scott’s life as hard as possible by carefully selecting the team he is going to manage in the company.
John knows which pairs of his people work poorly in the same team. John introduced a hardness factor of a team — it is a number of pairs of people from this team who work poorly in the same team divided by the total number of people in the team. The larger is the hardness factor, the harder is this team to manage. John wants to find a group of people in the company that are hardest to manage and make it Scott’s team. Please, help him.
In the example on the picture the hardest team consists of people 1, 2, 4, and 5. Among 4 of them 5 pairs work poorly in the same team, thus hardness factor is equal to 5⁄4. If we add person number 3 to the team then hardness factor decreases to 6⁄5.
Input
The first line of the input file contains two integer numbers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 1000). Here n is a total number of people in the company (people are numbered from 1 to n), and m is the number of pairs of people who work poorly in the same team. Next m lines describe those pairs with two integer numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) on a line. The order of people in a pair is arbitrary and no pair is listed twice.
Output
Write to the output file an integer number k (1 ≤ k ≤ n) — the number of people in the hardest team, followed by k lines listing people from this team in ascending order. If there are multiple teams with the same hardness factor then write any one.
Sample Input
sample input #1
5 6
1 5
5 4
4 2
2 5
1 2
3 1 sample input #2
4 0
Sample Output
sample output #1
4
1
2
4
5 sample output #2
1
1
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn<<];
int head[maxn],d[maxn],cur[maxn],x[maxn],y[maxn];
int tot,S,T,n,m;
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
queue<int>q;
q.push(S);
d[S] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow>&&d[e[i].to] == d[u]+ && (a=dfs(e[i].to,min(e[i].flow,low))) > ){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double flow = m;
while(bfs()){
memcpy(cur,head,sizeof(head));
double tmp = dfs(S,INF);
if(tmp > ) flow -= tmp;
}
return flow <= ;
}
void build(double delta){
memset(head,-,sizeof(head));
tot = ;
for(int i = ; i <= m; ++i){
add(S,i+n,1.0);
add(i+n,x[i],INF);
add(i+n,y[i],INF);
}
for(int i = ; i <= n; ++i) add(i,T,delta);
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + m + ;
for(int i = ; i <= m; ++i)
scanf("%d %d",x+i,y+i);
if(m == ) printf("1\n1\n");
else{
double low = ,high = 1.0*m,mid;
const double exps = 1.0/(n*n);
while(high - low >= exps){
mid = (low + high)/2.0;
build(mid);
if(dinic()) high = mid;
else low = mid;
}
build(low);
dinic();
int cnt = ,ans[maxn];
for(int i = ; i <= n; ++i)
if(d[i] > ) ans[cnt++] = i;
printf("%d\n",cnt);
for(int i = ; i < cnt; ++i)
printf("%d%c",ans[i],'\n');
}
}
return ;
}
第二种建模方案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],cur[maxn],d[maxn],du[maxn];
int tot,S,T,n,m,cnt;
pii p[maxn*maxn];
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S] = ;
queue<int>q;
q.push(S);
cnt = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
cnt++;
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > && d[u] + == d[e[i].to]&&(a=dfs(e[i].to,min(e[i].flow,low)))>){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double ans = n*m;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans -= dfs(S,INF);
}
return ans/2.0 > ;
}
void build(double g){
memset(head,-,sizeof(head));
for(int i = tot = ; i < m; ++i){
add(p[i].first,p[i].second,);
add(p[i].second,p[i].first,);
}
for(int i = ; i <= n; ++i){
add(S,i,m);
add(i,T,m+g*2.0-du[i]);
}
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + ;
memset(du,,sizeof(du));
for(int i = ; i < m; ++i){
scanf("%d %d",&p[i].first,&p[i].second);
++du[p[i].first];
++du[p[i].second];
}
if(!m) printf("1\n1\n");
else{
const double exps = 1.0/(n*n);
double low = ,high = m;
while(high - low >= exps){
double mid = (low + high)/2.0;
build(mid);
if(dinic()) low = mid;
else high = mid;
}
build(low);
dinic();
printf("%d\n",cnt);
for(int i = ; i <= n; ++i)
if(d[i] > -) printf("%d\n",i);
}
}
return ;
}
POJ 3155 Hard Life的更多相关文章
- POJ 3155 Hard Life(最大密度子图+改进算法)
Hard Life Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9012 Accepted: 2614 Case Ti ...
- POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分
http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...
- POJ 3155 Hard Life(最大密度子图)
裸题.输入一个无向图,输出最大密度子图(输出子图结点数和升序编号). 看了<最小割模型在信息学竞赛中的应用——胡伯涛>的一部分,感觉01分数规划问题又是个大坑.暂时还看不懂. 参考http ...
- poj 3155 最大密度子图
思路: 这个还是看的胡伯涛的论文<最小割在信息学竞赛中的应用>.是将最大密度子图问题转化为了01分数规划和最小割问题. 直接上代码: #include <iostream> # ...
- poj 3155 二分+最小割求实型最小割(最大密集子图)
/* 最大密集子图子图裸题 解法:设源点s和汇点t 根据胡波涛的<最小割模型在信息学中的应用> s-每个点,权值为原边权和m, 每个点-t,权值为m+2*g-degree[i], 原来的边 ...
- POJ 3155:Hard Life(最大密度子图)
题目链接 题意 给出n个人,和m对有冲突的人.要裁掉一些人,使得冲突率最高,冲突率为存在的冲突数/人数. 思路 题意可以转化为,求出一些边,使得|E|/|V|最大,这种分数规划叫做最大密度子图. 学习 ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
- POJ题目分类推荐 (很好很有层次感)
著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...
随机推荐
- js(Mandango:壮汉专用,电影院划位工具)
Mandango:壮汉专用,电影院划位工具 <body onload="initSeats();"> <div style="margin-top:75 ...
- js异步队列之理解
起因 最近看到一篇关于js异步执行顺序的解答,觉得有所收获,遂记录下来. marcotask和microtask js中异步队列可以分为两类,marcotask队列和microtask队列, marc ...
- 小学生都能学会的python(文件操作)
小学生都能学会的python(文件操作) 1. open("文件路径", mode="模式", encoding="编码") 文件的路径: ...
- SpringBoot中使用过滤器Filter
场景:API的参数都是经过加密的,于是在过滤器中,将获取到的请求的参数先解密再去进行处理 一.实现Filter接口 public class TestFilter implements Filter ...
- 常用类属于哪些jar包
1.@requestmapping注解,属于org.springframework.web.bind.annotation包下.org.springframework.web jar包. 2.@Res ...
- 使用excel进行数据挖掘(3)----类别检測
使用excel进行数据挖掘(3)----类别检測 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/artic ...
- iOS开发自己定义键盘回车键Return Key
在iOS开发中.用户在进行文本输入的时候,往往会用到虚拟键盘上的回车键,也就是Return Key.回车键有时候能够是"完毕"(表示输入结束).能够是"下一项" ...
- Android 输入管理服务-输入事件向详细应用的分发
输入管理服务接收到输入事件,对输入事件进行处理之后会把输入事件分发到详细的应用中(如WMS.壁纸服务等)去处理的,这里涉及到了JNI从C++层向JAVA层的调用. 详细流程例如以下图所看到的:
- windowsclient崩溃分析和调试
本文介绍windows上崩溃分析的一些手段,顺便提多进程调试.死锁等. 1.崩溃分析过程 1.1 确认错误码 不管是用windbg还是用vs.首先应该注意的是错误码,而90%以上的崩溃都是非法訪问. ...
- Django shortcut functions
django.shortcuts package提供提供帮助类和函数可以更便捷的操作MVC中的每一部分,包含: render(request, template_name,[dictionary],[ ...