题意:给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和整数d,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值非负且最大。
析:首先二分答案,很容易想到,令sum(u) 表示作用在 u 结点的所有d的和,然后对于一条 u 到 v 的边,要满足大于 ans,也就是sum(v) - sum(u) < w(u, v) - ans,然后可以形成很多个不等式组,就是可以用差分约束来做了,每次只要判断是不是有负环就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 500 + 10; const int mod = 1000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r > 0 && r <= n && c > 0 && c <= m; } struct Edge{ int from, to, dist; }; struct BellmanFord{ int n, m; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; int cnt[maxn]; int d[maxn]; void init(int n){ this-> n = n; for(int i = 0; i < n; ++i) G[i].cl; edges.cl; } void addEdge(int u, int v, int d){ edges.pb((Edge){u, v, d}); m = edges.sz; G[u].pb(m-1); } bool bfs(){ queue<int> q; ms(inq, 0); ms(cnt, 0); ms(d, 0); inq[0] = true; for(int i = 0; i < n; ++i) q.push(i); while(!q.empty()){ int u = q.front(); q.pop(); inq[u] = 0; for(int i = 0; i < G[u].sz; ++i){ Edge &e = edges[G[u][i]]; if(d[e.to] > d[u] + e.dist){ d[e.to] = d[u] + e.dist; if(!inq[e.to]){ q.push(e.to); inq[e.to] = 1; if(++cnt[e.to] > n) return true; } } } } return false; } bool solve(int x){ for(int i = 0; i < edges.sz; ++i) edges[i].dist -= x; bool ans = bfs(); for(int i = 0; i < edges.sz; ++i) edges[i].dist += x; return ans; } }; BellmanFord bell; int main(){ while(scanf("%d %d", &n, &m) == 2){ bell.init(n); int l = 1, r = 0; for(int i = 0; i < m; ++i){ int u, v, c; scanf("%d %d %d", &u, &v, &c); --u, --v; bell.addEdge(u, v, c); r = max(r, c); } if(bell.solve(l)) puts("No Solution"); else if(!bell.solve(r+1)) puts("Infinite"); else{ while(l <= r){ int m = l + r >> 1; if(bell.solve(m)) r = m - 1; else l = m + 1; } printf("%d ", l-1); } } return 0; }