HDU5988 - 2016icpc青岛 - G - Coding Contest 费用流(利用对数化乘为加

HDU5988

题意:

  有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 << "\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;
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 '\n' #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; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
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=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------show time----------------------*/
const int maxn = 1e6+;
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 = ,n,m; bool spfa(int s,int t){
//memset(gdist,inf,sizeof(gdist));
for(int i=; i<=n+; i++) {
gpre[i] = -;
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!=-; e= gedge[e].nxt){
int v = gedge[e].to;
double w = gedge[e].cost;
if(gedge[e].val > && gdist[v] - gdist[u] - w > 1e-){
gdist[v] = gdist[u] + w;
gpre[v] = u;
gpath[v] = e;
if(!in[v]){
que.push(v); in[v] = true;
}
}
}
} if(gpre[t] == -) return false;
return true;
} double MinCostFlow(int s,int t){
double cost = 0.0;int flow = ;
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] ^ ].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 = ;
gedge[gcount].cost = -cost;
gedge[gcount].nxt = h[v];
h[v] = gcount++;
}
int main(){
int T; scanf("%d", &T);
while(T--){
memset(h,-,sizeof(h)); gcount = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++){
int u,v;
scanf("%d%d", &u, &v);
if(u > v) addedge(,i,u-v,0.0);
else if(u < v) addedge(i,n+,v-u,0.0);
}
for(int i=; i<=m; i++){
int u,v,c;double p;
scanf("%d%d%d%lf", &u, &v, &c, &p);
if(c == )continue;
addedge(u,v,,0.0);
addedge(u,v,c-,-1.0*log(-p));
}
double ans = -1.0*MinCostFlow(,n+); printf("%.2f\n", -exp(ans));
} return ;
}

HDU5988

上一篇:hdu5441 并查集+克鲁斯卡尔算法


下一篇:java知识点深挖