UVA 11478 bf+差分約束系統+二分逼近

來源:互聯網
上載者:User

標籤:

Halum

You are given a directed graph G(V, E) with a set of vertices and edges. Each edge (i, j) that connects some vertex i to vertex j has an integer cost associated with that edge. Define the operation Halum(v, d) to operate on a vertex v using an integer d as follows: subtract d from the cost of all edges that enter v and add d to the cost of every edge that leaves v. As an example of that operation, consider graph G that has three vertices named (1, 2, 3) and two edges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2, −3) operates on edges entering and leaving vertex 2. Thus, edge (1, 2) gets cost -1-(-3)=2 and the edge (2, 3) gets cost 1 + (-3) = -2. Your goal is to apply the Halum function to a graph, potentially repeatedly, until every edge in the graph has at least a certain cost that is greater than zero. You have to maximize this cost.

Input

Two space-separated integers per case: V (V ≤ 500) and E (E ≤ 2700). E lines follow. Each line represents a directed edge using three space-separated integers (u, v, d). Absolute value of cost can be at most 10000.

Output

If the problem is solvable, then print the maximum possible value. If there is no such solution print ‘No Solution’. If the value can be arbitrary large print ‘Infinite’

Sample Input

2 1

1 2 10

2 1

1 2 -10

3 3

1 2 4

2 3 2

3 1 5

4 5

2 3 4

4 2 5

3 4 2

3 1 0

1 2 -1

Sample Output

Infinite

Infinite

3

1

思路:對於同一個節點,將多次操作合并,令sum(u)為u上所有節點的操作,問題轉化為每條求權值均不小於x,對於a->b,操作完,它的權值為w(a,b)+sum(a)-sum(b)>=x,移項得sum(b)-sum(a)<=w(a,b)+x,得到可圖個差分約束系統

差分約束系統可以用最短路徑演算法:對於約束條件xj-xi>=bk,建立一條邊i->j,權值為bk,再加一個源點,從s出發與所有其他點相連,權值為0,再加一個源點s,從s出發與其他點相連,權值為0。在這個圖上運行bf演算法,則源點s到所有點i的距離就是xi的值。如果bf演算法運行失敗,即圖中有負權環,則差分約束系統無解。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <string>#include <queue>#include <vector>#include <algorithm>#include <ctime>using namespace std;//#define EdsonLin#ifdef EdsonLin#define debug(...) fprintf(stderr,__VA_ARGS__)#else#define debug(...)#endif // EdsonLintypedef long long ll;typedef double db;const ll inf = 10010;const int MAXN = 500;const int MAXNN = 3e3+10;const ll MOD = 1000000007;const db eps = 1e-3;struct bf{    int n,m;    int first[MAXN];    struct edge{        int st,to,next,dist;    }e[MAXNN];    int top;    int d[MAXN];    int inq[MAXN];    int cnt[MAXN];   /* bf(int n){        this->n = n;        memset(first,-1,sizeof(first));        top = 0;    }*/    void init(int n){        this->n = n;        memset(first,-1,sizeof(first));        top = 0;    }    void addege(int u,int v,int dist){        e[top].st = u;        e[top].to = v;        e[top].dist = dist;        e[top].next = first[u];        first[u] = top++;    }    bool negativeCycle(){        queue<int>Q;        for(int i=0;i<n;i++){            Q.push(i);            cnt[i] = inq[i] = d[i] = 0;        }        inq[0] = 1;        while(!Q.empty()){            int u=Q.front();            Q.pop();            inq[u] = 0;            for(int i=first[u];i!=-1;i=e[i].next){                int v = e[i].to;                if(d[v]>d[u]+e[i].dist){                    d[v] = d[u]+e[i].dist;                    if(!inq[v]){                        cnt[v]++;                        inq[v] = 1;                        Q.push(v);                        if(cnt[v]>n){                            //cout<<"haha"<<endl;                            return true;                        }                    }                }            }        }        return false;;    }} solver;bool solve(int x){    bool sg;    for(int i=0;i<solver.top;i++){        solver.e[i].dist -= x;        //cout<<solver.e[i].dist<<endl;    }    sg = solver.negativeCycle();    for(int i=0;i<solver.top;i++){        solver.e[i].dist += x;       // cout<<solver.e[i].dist<<endl;    }    return sg;}int main(){    #ifdef EdsonLin        //freopen("1.in","r",stdin);        //freopen("1.out","w",stdout);        int _time_jc = clock();    #endif // EdsonLin    int n,m;    while(cin>>n>>m){        int u,v,dist;        solver.init(n);        for(int i=0;i<m;i++){            scanf("%d%d%d",&u,&v,&dist);            u--;v--;            solver.addege(u,v,dist);        }        if(!solve(inf)){            cout<<"Infinite"<<endl;            continue;        }        if(solve(1)){            cout<<"No Solution"<<endl;            continue;        }        int L,R,M;        R = inf,L = 1;        while(L<R){            M = L+(R-L+1)/2;            if(solve(M))R = M-1;            else L = M;        }        cout<<L<<endl;    }    #ifdef EdsonLin        debug("time: %d\n",int(clock()-_time_jc));    #endif // EdsonLin    //cout << "Hello world!" << endl;    return 0;}
bf模板

 

UVA 11478 bf+差分約束系統+二分逼近

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.