Codeforces 1131D Gourmet choice——差分约束

题意:给定一张比较表,给行列赋值使得比较表成立,并且最大值最小

思路:差分约束,求解最长路

/**
* x > y : x >= y + 1;
* x < y : x <= y - 1;
* x = y : x >= y x <= y
* maxlongpath: u -> v :
* if (dis[v] < dis[u] + cost) dis[v] = dis[u] + cost :
* dis[v] >= dis[u] + cost(u, v);
* x > y : y -> x cost = 1;
* x < y : x -> y cost = 1;
* x = y : x -> y, cost = 0, y -> x, cost = 0;
*/
#include 
using namespace std;
const int maxn = 1005;
int n, m;
char c;
typedef pair P;
vector

vec[maxn*2]; int du[maxn*2]; int dis[maxn*2], vis[maxn*2], cnt[maxn*2]; const int INF = 0x3f3f3f3f; bool spfa(int s) { memset(dis, -1, sizeof(dis)); memset(vis, 0, sizeof(vis)); queue que; que.push(s); dis[s] = 0; vis[s] = 1; cnt[s] = 1; while (!que.empty()) { int u = que.front(); que.pop(); vis[u] = 0; for (int i = 0; i < vec[u].size(); i++) { int v = vec[u][i].first, cost = vec[u][i].second; if (dis[v] < dis[u] + cost) { dis[v] = dis[u] + cost; if (!vis[v]) { cnt[v]++; if (cnt[v] >= n+m+1) return 0; vis[v] = 1; que.push(v); } else { } } } } return 1; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { getchar(); for (int j = 1; j <= m; j++) { scanf("%c", &c); if (c == '>') { vec[j+n].push_back(make_pair(i, 1)); } else if (c == '<') { vec[i].push_back(make_pair(j+n, 1)); } else { vec[j+n].push_back(make_pair(i, 0)); vec[i].push_back(make_pair(j+n, 0)); } } } for (int i = 1; i <= n + m; i++) { vec[0].push_back(make_pair(i, 1)); } bool ok = spfa(0); if (ok) { for (int i = 1; i <= n + m; i++) { if (dis[i] == -1) { ok = false; break; } } } if (ok) { puts("Yes"); for (int i = 1; i <= n; i++) { printf("%d ", dis[i]); } printf("\n"); for (int i = n + 1; i <= n + m; i++) { printf("%d ", dis[i]); } printf("\n"); } else { puts("No"); } return 0; }

 

你可能感兴趣的:(图论)