UVA 11478 Halum(差分約束系統+Bellman-Ford),11478halum

來源:互聯網
上載者:User

UVA 11478 Halum(差分約束系統+Bellman-Ford),11478halum


題意:給定一個有向圖,每條邊都有一個權值。每次你可以選擇一個結點v和一個整數d,把所有以v為終點的邊的權值減小d,把所有以v為起點的邊的權值增加d,最後讓所有邊的權值的最小值大於零且盡量大。

ps:lrj的書上有個坑,說上說非負,其實原題說要大於0.....wa了好幾發

分析:因為不同的操作互不影響,因此可以按任意順序實施這些操作。另外,對於同一個點的多次操作可以合并,因此可以令sum(u)為作用於結點u之上的所有d之和。這樣,本題的目標就是確定所有的sum(u),使得操作之後所有邊權的最小值盡量大。

“最小值最大”使用二分答案的方法。二分答案x之後,問題轉化為是否可以讓操作完畢後每條邊的權值均不小於x。對於邊a->b,不難發現操作完畢後它的權值為w(a,b)+sum(a)-sum(b),因此每條邊a->b都可以列出一個不等式w(a,b)+sum(a)-sum(b)>=x,移項得sum(b)-sum(a)<=w(a,b)-x。這樣,我們實際得到一個差分約束系統。

查分約束系統是指一個不等式組,每個不等式形如xj-xi<=bk,這裡的bk是一些事先已知的常數。這個不等式類似於最短路中的不等式d[v]<=d[u]+w(u,v),我們可以用最短路演算法求解:對於約束條件xj-xi《=bk,建立一條邊i-->j,權值為bk。如果圖中有負權環,則差分約束系統無解。

#include<cstdio>  #include<cstring>  #include<cmath>  #include<cstdlib>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<queue>  #include<stack> #include<string>#include<map> #include<set>#define eps 1e-4 #define LL long long  using namespace std;  const int maxn = 500 + 10;const int INF = 0x3f3f3f3f;//Bellman-Ford演算法struct Edge {int from, to;int dist;Edge(int u = 0, int v = 0, double d = 0) : from(u), to(v), dist(d) {}};struct BellmanFord{int n, m;vector<Edge> edges;vector<int> G[maxn];bool inq[maxn];int d[maxn];int p[maxn];int cnt[maxn];void init(int n) {this->n = n;for(int i = 0; i < n; i++) G[i].clear();edges.clear();}void AddEdges(int from, int to, int dist) {edges.push_back(Edge(from, to, dist));m = edges.size();G[from].push_back(m-1);}bool bellman_ford(int S) {queue<int> Q;memset(inq, 0, sizeof(inq));memset(cnt, 0, sizeof(cnt));for(int i = 0; i < n; i++) d[i] = INF;d[S] = 0;inq[S] = true;Q.push(S);while(!Q.empty()) {int u = Q.front(); Q.pop();inq[u] = false;for(int i = 0; i < G[u].size(); i++) {Edge& e = edges[G[u][i]];if(d[u]<INF && d[e.to]>d[u]+e.dist) {d[e.to] = d[u] + e.dist;p[e.to] = G[u][i];if(!inq[e.to]) {Q.push(e.to);inq[e.to] = true;if(++cnt[e.to] > n) return false;}}}}return true;}bool negetiveCycle() {queue<int> Q;memset(inq, 0, sizeof(inq));memset(cnt, 0, sizeof(cnt));for(int i = 0; i < n; i++) {d[i] = 0; inq[0] = true; Q.push(i);}while(!Q.empty()) {int u = Q.front(); Q.pop();inq[u] = false;for(int i = 0; i < G[u].size(); i++) {Edge& e = edges[G[u][i]];if(d[e.to]>d[u]+e.dist) {d[e.to] = d[u] + e.dist;p[e.to] = G[u][i];if(!inq[e.to]) {Q.push(e.to);inq[e.to] = true;if(++cnt[e.to] > n) return true;}}}}return false;}} solver; int n, m;bool test(int w) {for(int i = 0; i < m; i++) solver.edges[i].dist -= w;int t = solver.negetiveCycle();for(int i = 0; i < m; i++) solver.edges[i].dist += w;return t;}int kase;int main() {freopen("input.txt", "r", stdin);while(scanf("%d%d", &n, &m) == 2) {solver.init(n);for(int i = 0; i < m; i++) {int u, v, d;cin >> u >> v >> d;u--; v--;solver.AddEdges(u, v, d);}int L = 0, R = 10000;if(test(1)) puts("No Solution");else if(!test(R+5)) puts("Infinite");else {while(L <= R) {int M = (R+L)/2;if(test(M)) R = M - 1;else L = M + 1;}printf("%d\n", R);}}return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.