0-1规划 UVA 1389 Hard Life

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 harderst to manage and make it Scott's team. Please, help him.

\epsfbox{p3709.eps}

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 $ {\frac{​{5}}{​{4}}}$ . If we add person number 3 to the team then hardness factor decreases to $ {\frac{​{6}}{​{5}}}$ .

Input 

The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.


The first line of the input contains two integer numbers n and m (1$ \le$n$ \le$100, 0$ \le$m$ \le$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 andbi (1$ \le$aibi$ \le$nai $ \neq$ bi) on a line. The order of people in a pair is arbitrary and no pair is listed twice.

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


Write to the output an integer number k (1$ \le$k$ \le$n) -- the number of people in the hardest team, followed byk lines listing people from this team in ascending order. If there are multiple teams with the same hardness factor then write any one.

Note, that in the last example any team has hardness factor of zero, and any non-empty list of people is a valid answer.

Sample Input 

5 6 
1 5 
5 4 
4 2 
2 5 
1 2 
3 1 

4 0

Sample Output 

4 
1 
2 
4 
5 

1 
1


具体可以看胡伯涛的论文。。。我就不多说了


代码:

#include <iostream>
#include <cstring>
#include <string.h>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define eps 1e-8
const int maxn=1000+100+5;
const double inf=1e10;

struct Edge
{
    int u,v;
    double cap,flow;
    Edge(int u,int v,double cap,double flow=0)
    :u(u),v(v),cap(cap){ }
};

vector<Edge> edges;
vector<int> G[maxn];

void add(int u,int v,double c)
{
    edges.push_back(Edge(u,v,c));
    edges.push_back(Edge(v,u,0));
    int m=edges.size();
    G[u].push_back(m-2);
    G[v].push_back(m-1);
}

int dcmp(double a,double b)
{
    if(a>b+eps) return 1;
    else if(a<b-eps) return -1;
    return 0;
}
struct Dinic
{
    int q[maxn],front,rear;
    int d[maxn],cur[maxn];
    int n,s,t;
    void init(int n) { this->n=n; }
    bool bfs()
    {
        front=rear=0;
        q[rear++]=s;
        for(int i=0;i<n;++i) d[i]=n;
        d[s]=0;
        while(front<rear) {
            int u=q[front++];
            for(int i=0;i<G[u].size();++i) {
                Edge&e=edges[G[u][i]];
                int v=e.v;
                if(e.cap>e.flow+eps&&d[v]>d[u]+1) {
                    d[v]=d[u]+1;
                    q[rear++]=v;
                }
            }
        }
        return d[t]<n;
    }
    double dfs(int u,double a)
    {
        if(u==t||a<eps) return a;
        double ret=0,f;
        for(int&i=cur[u];i<G[u].size();++i) {
            Edge&e=edges[G[u][i]];
            int v=e.v;
            if(dcmp(e.cap,e.flow)<=0||d[v]!=d[u]+1) continue;
            f=dfs(v,min(a,e.cap-e.flow));
            if(f<eps) continue;
            e.flow+=f;
            edges[G[u][i]^1].flow-=f;
            a-=f; ret+=f;
            if(a<eps) break;
        }
        return ret;
    }
    double maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        double flow=0;
        while(bfs()) {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,inf);
        }
        return flow;
    }
}solver;

int n,m,s,t,e;
void input()
{
    edges.clear();
    for(int i=0;i<maxn;++i) G[i].clear();
    s=0; t=n+m+1;
    solver.init(n+m+2);
    for(int i=1;i<=m;++i) {
        int u,v; scanf("%d%d",&u,&v);
        add(s,i,1);
        add(i,u+m,inf);
        add(i,v+m,inf);
    }
    e=edges.size();
    for(int i=m+1;i<=n+m;++i) add(i,t,0);
}

void clear(double c)
{
    for(int i=0;i<edges.size();++i) edges[i].flow=0;
    for(int i=e;i<edges.size();i+=2) edges[i].cap=c;
}

bool vst[maxn];

int dfs(int u)
{
    if(vst[u]) return 0;
    int cnt=0;
    if(u<=n+m&&u>=m+1) ++cnt;
    vst[u]=true;
    for(int i=0;i<G[u].size();++i) {
        Edge&e=edges[G[u][i]];
        if(vst[e.v]) continue;
        if(e.cap>e.flow)  cnt+=dfs(e.v);
    }
    return cnt;
}

void solve()
{
    if(m==0) { printf("1\n1\n"); return; }
    double left=0,right=m;
    while(right-left>=(1.0/n/n)) {
        double m=(left+right)*0.5;
        clear(m);
        double f=solver.maxflow(s,t);
        double x=::m-f;
        if(x>eps) left=m;
        else right=m;
    }
    clear(left);
    solver.maxflow(s,t);
    memset(vst,0,sizeof(vst));
    int cnt=dfs(s);
    printf("%d\n",cnt);
    for(int i=m+1;i<=n+m;++i)
    if(vst[i]) printf("%d\n",i-m);
}

int main()
{
    bool first=true;
    while(scanf("%d%d",&n,&m)==2) {
        if(first) first=false;
        else puts("");
        input();
        solve();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值