P3705 [SDOI2017]新生舞会 分数规划 费用流

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3705 [SDOI2017]新生舞会 分数规划 费用流相关的知识,希望对你有一定的参考价值。

技术图片

技术图片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

/*
        
⊂_ヽ
  \\ Λ_Λ  来了老弟
   \(‘?‘)
    > ⌒ヽ
   /   へ\
   /  / \\
   ? ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
‘ノ )  L?

*/

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘
‘

#define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);


const ll oo = 1ll<<17;
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}
struct FastIO {
    static const int S = 4e6;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar() {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) exit(0);
        return buf[pos++];
    }
    inline int xuint() {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; 0 <= c && c <= 9; c = xchar()) x = x * 10 + c - 0;
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == -) s = -1, c = xchar();
        for (; 0 <= c && c <= 9; c = xchar()) x = x * 10 + c - 0;
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos++] = x;
    }
    inline void wint(int x)
    {
        if (x < 0) wchar(-), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n++] = 0 + x % 10, x /= 10;
        while (n--) wchar(s[n]);
        wchar(
);
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;   
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}

/*-----------------------showtime----------------------*/
            const int maxn = 209;
            int n;
            int a[maxn][maxn],b[maxn][maxn];

            struct E{
                int v,val;
                double cost;
                int nxt;
            }edge[maxn*maxn];
            
            int head[maxn],gtot = 0;
            void addedge(int u,int v,int val,double cost){
                edge[gtot].v = v;
                edge[gtot].val = val;
                edge[gtot].cost = cost;
                edge[gtot].nxt = head[u];
                head[u] = gtot++;

                edge[gtot].v = u;
                edge[gtot].val = 0;
                edge[gtot].cost = -cost;
                edge[gtot].nxt = head[v];
                head[v] = gtot++;   
            }

            double dis[maxn];
            int vis[maxn],pre[maxn],path[maxn];
            bool spfa(int s,int t){
                for(int i=s; i <=t; i++) dis[i] = 1000000000.0;
                memset(pre, -1, sizeof(pre));
                memset(vis, 0, sizeof(vis));
                queue<int>que;
                que.push(s); vis[s] = 1;
                dis[s] = 0.0;
                while(!que.empty()){
                    int u = que.front(); que.pop();
                    vis[u] = 0;
                    for(int i=head[u]; ~i; i = edge[i].nxt){
                        int v = edge[i].v, val = edge[i].val;
                        double cost = edge[i].cost;
                        if(val > 0 && dis[v] > dis[u] + cost){
                            dis[v] = dis[u] + cost;
                            pre[v] = u; path[v] = i;
                            if(vis[v] == 0){
                                que.push(v);
                                vis[v] = 1;
                            }
                        }
                    }
                }
                return pre[t] != -1;
            }
            double mcmf(int s,int t){
                int flow = 0;
                double cost = 0;
                while(spfa(s, t)){
                    int f = inf;
                    for(int i=t; i!=s; i=pre[i]){
                        f = min(f, edge[path[i]].val);
                    }
                    flow += f;
                    cost = cost + 1.0*f*dis[t];
                    for(int i=t; i!=s; i=pre[i]){
                        edge[path[i]].val -=f;
                        edge[path[i]^1].val += f;
                    }
                }
                return cost;
            }
            bool check(double c){
                memset(head, -1, sizeof(head));
                gtot = 0;
                int s = 0, t = n+n+1;
                for(int i=1; i<=n; i++) {
                    addedge(s, i, 1, 0);
                    for(int j=1; j<=n; j++){
                        addedge(i, j+n, 1, 1.0*c * b[i][j] - 1.0*a[i][j]);
                    }
                    addedge(i+n, t, 1, 0);  
                }
                return mcmf(s, t) < 0.0;
            }
int main(){
            n = io.xint();
            rep(i, 1, n) rep(j,1,n) a[i][j] = io.xint() ;
            rep(i, 1, n) rep(j,1,n) b[i][j] = io.xint();
            double le = 0,ri = 1000000;
            while(abs(ri - le) > esp){

                double mid = (le + ri)*1.0/2.0;
           //     debug(mid);
                if(check(mid)) le = mid;
                else ri = mid;
            }
            printf("%.6f
", le);

            return 0;
}
View Code

 

以上是关于P3705 [SDOI2017]新生舞会 分数规划 费用流的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ4819: [Sdoi2017]新生舞会(01分数规划)

BZOJ_4819_[Sdoi2017]新生舞会_01分数规划+费用流

bzoj4819[Sdoi2017]新生舞会 分数规划+费用流

BZOJ.4819.[SDOI2017]新生舞会(01分数规划 费用流SPFA)

[SDOI2017]新生舞会

codevs 5965 [SDOI2017]新生舞会