P3705 [SDOI2017]新生舞會 分數規劃 費用流
阿新 • • 發佈:2019-02-18
getch () closed std names hid getc span opened
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #includeView Code<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 << "\n"; #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 ‘\n‘ #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(‘\n‘); } 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\n", le); return 0; }
P3705 [SDOI2017]新生舞會 分數規劃 費用流