1. 程式人生 > >UVALive - 7740 Coding Contest 2016 青島區域賽 (費用流)

UVALive - 7740 Coding Contest 2016 青島區域賽 (費用流)

endif bool namespace spa pre mat -- stdin main

題意:每個點i有\(s_i\)個人和\(b_i\)份食物,每個人都要找到一份食物.現在有M條有向邊,從點i到點j,容量為c,第一次走過不要緊,從第二次開始就要承擔\(p(0<p<1)\)的道路損壞的風險.題目保證每個人都能拿到食物,求這個風險的最小值.
分析:建立源點S和匯點T.從S到點i建容量為\(s_i\),費用為0的邊;從點i到T建容量為\(b_i\),費用為0的邊.
風險的最小值可以轉化為求安全達成目標的. 則\(ans = \prod (1-p_i)^{k_i}\),
對ans取對數,\(log(ans) = \sum k_i*log(1-p_i)\).因為需要求這個值最大,所以費用需要取反.
因為第一次走沒有風險,可以將這條容量為1的邊分離出來,若容量還有剩余則建容量為\(c-1\)

,費用為\(-log(1-p)\)的邊.
跑一遍費用流,費用取反再還原之後就是最大的安全通過的概率,1減去這個概率就是最小的風險.

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-7
const int MAXN = 10005;
const int MAXM = 100005;
const int INF = 0x3f3f3f3f;
struct Edge{
    int to, next, cap, flow;
    double cost;
} edge[MAXM];
int head[MAXN], tot;
int pre[MAXN];
double dis[MAXN];
bool vis[MAXN];
int N; 
void init(int n)
{
    N = n;
    tot = 0;
    memset(head, -1, sizeof(head));
}

void AddEdge(int u, int v, int cap, double cost)
{
    edge[tot] = (Edge){v,head[u],cap,0,cost};
    head[u] = tot++;
    edge[tot] = (Edge){u,head[v],0,0,-cost};
    head[v] = tot++;
}

bool spfa(int s, int t){
    queue<int> q;
    for (int i = 0; i <= N; i++){
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while (!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;

            if (edge[i].cap > edge[i].flow && dis[v] - (dis[u] + edge[i].cost)> eps){
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if (!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if (pre[t] == -1) return false;
    else  return true;
}

int minCostMaxflow(int s, int t, double &cost){
    int flow = 0;
    cost = 0;
    while (spfa(s, t)){
        int Min = INF;
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
            if (Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
            edge[i].flow += Min;
            edge[i ^ 1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
    return flow;
}

int have[MAXN];
int need[MAXN];


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d %d",&n,&m);
        init(n+2);
        int u,v; double w;
        int s= 0, t = n+1;
        for(int i=1 ; i<=n; ++i){
            scanf("%d %d", &need[i], &have[i]);
        }
        for(int i=1;i<=n;++i){
            AddEdge(s,i,need[i],0);
            AddEdge(i,t,have[i],0);
        }

        int c;
        double p;
        for(int i=1;i<=m;++i){
            scanf("%d %d %d %lf",&u, &v, &c, &p);
            if(c >= 1) AddEdge(u,v,1,0.0);
            if(c > 1) AddEdge(u,v,c-1,-log2(1.0-p));
        }
        double cost = 0.0;
        minCostMaxflow(s,t,cost);
        //cout<<cost<<endl;
        printf("%.2f\n",1.0-pow(2.0,-cost));
    }
    return 0;
}

UVALive - 7740 Coding Contest 2016 青島區域賽 (費用流)