标签:
今天刚刚学差分约束系统。利用最短路求解不等式。世界真的好奇妙!感觉不等式漏下几个会导致WA!!
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn = 1111; vector<int>ljb[maxn];//邻接表 int jz[maxn][maxn];//邻接矩阵 int n, m, shibai; int c[maxn], d[maxn]; int dist[maxn]; int flag[maxn], tott[maxn]; void spfa() { int ii; queue<int>Q; memset(flag, 0, sizeof(flag)); memset(tott, 0, sizeof(tott)); flag[n] = 1; tott[n]++; for (ii = 0; ii <= n; ii++) dist[ii] = 999999999; dist[n] = 0; Q.push(n); while (!Q.empty()) { int hh = Q.front(); Q.pop(); flag[hh] = 0; for (ii = 0; ii < ljb[hh].size(); ii++) { if (jz[hh][ljb[hh][ii]] != 999999999) { if (dist[hh] + jz[hh][ljb[hh][ii]] < dist[ljb[hh][ii]]) { dist[ljb[hh][ii]] = dist[hh] + jz[hh][ljb[hh][ii]]; if (flag[ljb[hh][ii]] == 0) { Q.push(ljb[hh][ii]); flag[ljb[hh][ii]] = 1; tott[ljb[hh][ii]]++; if (tott[ljb[hh][ii]] >= n) { shibai = 1; break; } } } } } if (shibai) break; } } int main() { int i, j, u, v, k; while (~scanf("%d%d", &n, &m)) { shibai = 0; c[0] = 0; d[0] = 0; for (i = 0; i <= n; i++) ljb[i].clear(); for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (i == j) jz[i][j] = 0; else jz[i][j] = 999999999; } } for (i = 1; i <= n; i++) scanf("%d", &c[i]); for (i = 1; i <= n; i++) d[i] = c[i] + d[i - 1]; for (i = 0; i < m; i++) { scanf("%d%d%d", &v, &u, &k); jz[u][v - 1] = -k; ljb[u].push_back(v - 1); jz[v - 1][u] = d[u] - d[v - 1]; ljb[v - 1].push_back(u); } for (i = 1; i <= n; i++) { jz[i - 1][i] = c[i]; ljb[i - 1].push_back(i); jz[i][i - 1] = 0; ljb[i].push_back(i - 1); } spfa(); if (shibai == 1) printf("Bad Estimations\n"); else printf("%d\n", -dist[0]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/4518305.html