「SHOI2007」「Codevs2341」 善意的投票(最小割

2341 善意的投票

2007年省队选拔赛上海市队选拔赛

时间限制: 5 s
空间限制: 128000 KB
题目等级 : 大师 Master
 

题目描述 Description

幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。

我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?

输入描述 Input Description

文件的第一行只有两个整数n,m,保证有2≤n≤300,1≤m≤n(n-1)/2。其中n代表总人数,m代表好朋友的对数。文件第二行有n个整数,第i个整数代表第i个小朋友的意愿,当它为1时表示同意睡觉,当它为0时表示反对睡觉。接下来文件还有m行,每行有两个整数i,j。表示i,j是一对好朋友,我们保证任何两对i,j不会重复。

输出描述 Output Description

只需要输出一个整数,即可能的最小冲突数。

样例输入 Sample Input

3 3

1 0 0

1 2

1 3

3 2

样例输出 Sample Output

1

数据范围及提示 Data Size & Hint

2≤n≤300,1≤m≤n(n-1)/2。

题解

惯性思维真是个要死的东西

小M的作物的题解里有人说这两个题很像 于是思考陷入混乱(???

这个题还是简单很多的(大概是洗礼过一次了的原因

如果不想睡觉 从S连条1 否则往T连条1 这是违背自己意愿的代价

然后每对塑料姐妹花之间连双向边,边权为1 代表迁就的代价

然后跑最小割

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=2*1e9+1;
struct emm{
    int e,f,v;
}a[200007];
int h[307];
int tot=1;
int s,t;
void con(int x,int y,int w)
{
    a[++tot].f=h[x];
    h[x]=tot;
    a[tot].e=y;
    a[tot].v=w;
    a[++tot].f=h[y];
    h[y]=tot;
    a[tot].e=x;
    return;
}
queue<int>q;
int d[307];
inline bool bfs()
{
    memset(d,0,sizeof(d));
    d[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=h[x];i;i=a[i].f)
        if(!d[a[i].e]&&a[i].v)
        {
            d[a[i].e]=d[x]+1;
            q.push(a[i].e);
        }
    }
    return d[t];
}
int dfs(int x,int al)
{
    if(x==t||!al)return al;
    int fl=0;
    for(int i=h[x];i;i=a[i].f)
    if(d[a[i].e]==d[x]+1&&a[i].v)
    {
        int f=dfs(a[i].e,min(a[i].v,al));
        if(f)
        {
            fl+=f;
            al-=f;
            a[i].v-=f;
            a[i^1].v+=f;
            if(!al)break;
        }
    }
    if(!fl)d[x]=-1;
    return fl;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    s=0,t=n+1;
    for(int i=1;i<=n;++i)
    {
        int x;
        scanf("%d",&x);
        if(x==1)con(s,i,1);
        else con(i,t,1);
    }
    for(int i=1;i<=m;++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        con(x,y,1);
        con(y,x,1);
    }
    long long ans=0;
    while(bfs())ans+=dfs(s,INF);
    cout<<ans;
    return 0;
}
原文地址:https://www.cnblogs.com/qwerta/p/9535236.html