一个典型的01分数规划, 建图详见amber的论文 很详细了
这种题最有可能出问题的是二分精度。尤其是之前做过的一个密度子图的题。
所以我一般都二分完毕后再用low作为参数跑一遍
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> #define eps 1e-5 #define MAXN 222 #define MAXM 5555 #define INF 100000007 using namespace std; typedef double type; struct node { int v; type c, f; int next, r; }edge[MAXM]; int dist[MAXN], nm[MAXN], src, des, n; int head[MAXN], e; void add(int x, int y, type c) { edge[e].v = y; edge[e].c = c; edge[e].f = 0; edge[e].r = e + 1; edge[e].next = head[x]; head[x] = e++; edge[e].v = x; edge[e].c = 0; edge[e].f = 0; edge[e].r = e - 1; edge[e].next = head[y]; head[y] = e++; } void rev_BFS() { int Q[MAXN], h = 0, t = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; nm[i] = 0; } Q[t++] = des; dist[des] = 0; nm[0] = 1; while(h != t) { int v = Q[h++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].r].c == 0 || dist[edge[i].v] < MAXN)continue; dist[edge[i].v] = dist[v] + 1; ++nm[dist[edge[i].v]]; Q[t++] = edge[i].v; } } } void init() { e = 0; memset(head, -1, sizeof(head)); } type maxflow() { rev_BFS(); int u; type total = 0; int cur[MAXN], rpath[MAXN]; for(int i = 1; i <= n; ++i)cur[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { type tf = INF; for(int i = src; i != des; i = edge[cur[i]].v) tf = min(tf, edge[cur[i]].c); for(int i = src; i != des; i = edge[cur[i]].v) { edge[cur[i]].c -= tf; edge[edge[cur[i]].r].c += tf; edge[cur[i]].f += tf; edge[edge[cur[i]].r].f -= tf; } total += tf; u = src; } int i; for(i = cur[u]; i != -1; i = edge[i].next) if(edge[i].c > 0 && dist[u] == dist[edge[i].v] + 1)break; if(i != -1) // find an admissible arc, then Advance { cur[u] = i; rpath[edge[i].v] = edge[i].r; u = edge[i].v; } else // no admissible arc, then relabel this vtex { if(0 == (--nm[dist[u]]))break; // GAP cut, Important! cur[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].c > 0)mindist = min(mindist, dist[edge[j].v]); dist[u] = mindist + 1; ++nm[dist[u]]; if(u != src) u = edge[rpath[u]].v; // Backtrack } } return total; } int nt, m; int xx[MAXM], yy[MAXM], vis[MAXN], out[MAXM]; type cc[MAXM]; bool ok(double mid) { init(); double flow = 0; for(int i = 1; i <= m; i++) { if(cc[i] > mid) add(xx[i], yy[i], cc[i] - mid), add(yy[i], xx[i], cc[i] - mid); else flow += cc[i] - mid; } flow += maxflow(); if(flow < eps) return true; else return false; } void dfs(int u) { vis[u] = 1; for(int i = head[u]; i != -1; i = edge[i].next) if(edge[i].c > 0 && !vis[edge[i].v]) dfs(edge[i].v); } int main() { int cas = 0; while(scanf("%d%d", &nt, &m) != EOF) { if(cas++) printf("\n"); src = 1; des = nt; n = nt; double low = INF, high = 0; for(int i = 1; i <= m; i++) { scanf("%d%d%lf", &xx[i], &yy[i], &cc[i]); low = min(low, cc[i]); high = max(high, cc[i]); } while(high - low > eps) { double mid = (low + high) / 2; if(ok(mid)) high = mid; else low = mid; } ok(low); memset(vis, 0, sizeof(vis)); dfs(src); int cnt = 0; for(int i = 1; i <= m; i++) if(vis[xx[i]] + vis[yy[i]] == 1 || cc[i] <= low) out[cnt++] = i; printf("%d\n", cnt); for(int i = 0; i < cnt; i++) { printf("%d", out[i]); if(i < cnt - 1) putchar(' '); else putchar('\n'); } } return 0; }