【网络流】 csu 1623 Inspectors
2015-05-18 17:51
148 查看
本质上就是找环,建二分图跑费用流就可以了。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 205 #define maxm 500005 #define eps 1e-12 #define mod 998244353 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair //#define ls o<<1 //#define rs o<<1 | 1 //#define lson o<<1, L, mid //#define rson o<<1 | 1, mid+1, R //#pragma comment(linker, "/STACK:102400000,102400000") #define pii pair<int, int> typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head struct Edge { int v, c, w, next; Edge(int v = 0, int c = 0, int w = 0, int next = 0) : v(v), c(c), w(w), next(next) {} }E[maxm]; queue<int> q; int H[maxn], cntE; int vis[maxn]; int cap[maxn]; int dis[maxn]; int cur[maxn]; int n, m, flow, cost, s, t, T; void addedges(int u, int v, int c, int w) { E[cntE] = Edge(v, c, w, H[u]); H[u] = cntE++; E[cntE] = Edge(u, 0, -w, H[v]); H[v] = cntE++; } bool spfa() { memset(dis, INF, sizeof dis); cur[s] = -1; vis[s] = ++T; dis[s] = 0; cap[s] = INF; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = T - 1; for(int e = H[u]; ~e; e = E[e].next) { int v = E[e].v, c = E[e].c, w = E[e].w; if(c && dis[u] + w < dis[v]) { dis[v] = dis[u] + w; cap[v] = min(cap[u], c); cur[v] = e; if(vis[v] != T) { vis[v] = T; q.push(v); } } } } if(dis[t] == INF) return false; flow += cap[t]; cost += cap[t] * dis[t]; for(int e = cur[t]; ~e; e = cur[E[e ^ 1].v]) { E[e].c -= cap[t]; E[e ^ 1].c += cap[t]; } return true; } int mcmf() { flow = cost = 0; while(spfa()); return cost; } void init() { cntE = T = 0; memset(H, -1, sizeof H); memset(vis, 0, sizeof vis); } void work(int _) { init(); scanf("%d", &n); int w; s = 0, t = 2 * n + 1; for(int i = 1; i < n; i++) for(int j = i+1; j <= n; j++) { scanf("%d", &w); addedges(i, j+n, 1, w); addedges(j, i+n, 1, w); } for(int i = 1; i <= n; i++) addedges(s, i, 1, 0); for(int i = 1; i <= n; i++) addedges(i+n, t, 1, 0); printf("Case %d: %d\n", _, mcmf()); } int main() { int _; scanf("%d", &_); for(int i = 1; i <= _; i++) work(i); return 0; }
相关文章推荐
- CSU 1623 Inspectors(二分图最大权匹配 KM算法)(UVAlive 6879)
- CSU 1506 - Double Shortest Paths(网络流’最小费用流)
- 【CSUOJ 1623】Inspectors
- 关于CSU 1725以及CodeForce 148D自己的解法
- acm湖南第七届省赛 RMQ with Shifts (线段树) csu 1110
- CSU-1216: 异或最大值-trie-01字典树
- 【生活没有希望】poj1273网络流大水题
- CSU 1770 按钮控制彩灯实验
- CSU 1100: 一二三【模拟】
- CF 704D Captain America 上下界网络流
- csu 1105 NBUT 1108 打怪升级
- CSU 1558 和与积
- CSU 1112: 机器人的指令【模拟题】
- CSU 1018 Avatar
- CSU - 1178 BMW
- CSU - 1215 稳定排序
- POJ1273 Drainage Ditches (网络流)
- HDU 4292 Food 图论,网络流,建模
- UVA 10983 - Buy one, get the rest free(网络流)
- [CSU 1809: Parenthesis] 线段树/RMQ处理括号序列