HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加
Posted ckxkexing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加相关的知识,希望对你有一定的参考价值。
题意:
有n个区域,每个区域有s个人,b份饭。现在告诉你每个区域间的有向路径,每条路有容量和损坏路径的概率。问如何走可以使得路径不被破坏的概率最小。第一个人走某条道路是百分百不会损坏道路的。
思路:
对于每个人,他从起点到目的地,不损坏道路的概率是(1 - p【1】*p【2】...*p【r】)。相乘不好做,对减号右边的乘法取对数,就成了相加,就可以愉快的做相加运算了,就是可以跑费用流了。这道题spfa还有加入esp=1e-8。
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #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> 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; typedef pair<pii,int> pp3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ‘ ‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;} template<typename T>inline void mod_(T &A,ll MOD=mod) {A%=MOD; A+=MOD; A%=MOD;} 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; } /*-----------------------show time----------------------*/ const int maxn = 1e6+9; struct edge{ int to,val,nxt; double cost; }gedge[maxn]; int h[maxn],gpre[maxn]; int gpath[maxn]; double gdist[maxn]; bool in[maxn]; int gcount = 0,n,m; bool spfa(int s,int t){ //memset(gdist,inf,sizeof(gdist)); for(int i=0; i<=n+1; i++) { gpre[i] = -1; gdist[i] = 999999999.9; in[i] = false; } gdist[s] = 0.0; in[s] = true; queue<int>que; que.push(s); while(!que.empty()){ int u = que.front(); que.pop(); in[u] =false; for(int e = h[u]; e!=-1; e= gedge[e].nxt){ int v = gedge[e].to; double w = gedge[e].cost; if(gedge[e].val > 0 && gdist[v] - gdist[u] - w > 1e-8){ gdist[v] = gdist[u] + w; gpre[v] = u; gpath[v] = e; if(!in[v]){ que.push(v); in[v] = true; } } } } if(gpre[t] == -1) return false; return true; } double MinCostFlow(int s,int t){ double cost = 0.0;int flow = 0; while(spfa(s,t)){ double f = 999999999.99; for(int u=t; u!=s;u = gpre[u]){ if(gedge[gpath[u]].val < f){ f = gedge[gpath[u]].val; } } flow += f; cost += 1.0*gdist[t] * f; for(int u=t; u!=s; u = gpre[u]){ gedge[gpath[u]].val -= f; gedge[gpath[u] ^ 1].val += f; } } return cost; } void addedge(int u,int v,int val, double cost){ gedge[gcount].to = v; gedge[gcount].val = val; gedge[gcount].cost = cost; gedge[gcount].nxt = h[u]; h[u] = gcount++; gedge[gcount].to = u; gedge[gcount].val = 0; gedge[gcount].cost = -cost; gedge[gcount].nxt = h[v]; h[v] = gcount++; } int main(){ int T; scanf("%d", &T); while(T--){ memset(h,-1,sizeof(h)); gcount = 0; scanf("%d%d", &n, &m); for(int i=1; i<=n; i++){ int u,v; scanf("%d%d", &u, &v); if(u > v) addedge(0,i,u-v,0.0); else if(u < v) addedge(i,n+1,v-u,0.0); } for(int i=1; i<=m; i++){ int u,v,c;double p; scanf("%d%d%d%lf", &u, &v, &c, &p); if(c == 0)continue; addedge(u,v,1,0.0); addedge(u,v,c-1,-1.0*log(1-p)); } double ans = -1.0*MinCostFlow(0,n+1); printf("%.2f ", 1-exp(ans)); } return 0; }
以上是关于HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5988 Coding Contest(费用流+浮点数)