题意是求出图的一个边割集。。。是图的边割集不是网络流的边割集。。。使得边权平均值最小。。。解法就是01分数规划。。。amber的论上有讲的。。。
#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 405 #define maxm 800005 #define eps 1e-7 #define mod 1000000007 #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 #define pii pair<int, int> #pragma comment(linker, "/STACK:16777216") 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, next; double c; Edge(int v = 0, double c = 0, int next = 0) : v(v), c(c), next(next) {} }E[maxm]; queue<int> q; int H[maxn], cntE; int cur[maxn]; int pre[maxn]; int cnt[maxn]; int dis[maxn]; int s, t, nv; double flow; void addedges(int u, int v, double c) { E[cntE] = Edge(v, c, H[u]); H[u] = cntE++; E[cntE] = Edge(u, 0, H[v]); H[v] = cntE++; } void bfs() { memset(cnt, 0, sizeof cnt); memset(dis, -1, sizeof dis); q.push(t); dis[t] = 0, cnt[0] = 1; while(!q.empty()) { int u = q.front(); q.pop(); for(int e = H[u]; ~e; e = E[e].next) { int v = E[e].v; if(dis[v] == -1) { dis[v] = dis[u] + 1; cnt[dis[v]]++; q.push(v); } } } } double isap() { memcpy(cur, H, sizeof H); bfs(); flow = 0; int u = pre[s] = s, minv, pos, e; double f; while(dis[s] < nv) { if(u == t) { f = INF; for(int i = s; i != t; i = E[cur[i]].v) if(E[cur[i]].c < f) { f = E[cur[i]].c; pos = i; } for(int i = s; i != t; i = E[cur[i]].v) { E[cur[i]].c -= f; E[cur[i] ^ 1].c += f; } flow += f; u = pos; } for(e = cur[u]; ~e; e = E[e].next) if(E[e].c > eps && dis[u] == dis[E[e].v] + 1) break; if(~e) { cur[u] = e; pre[E[e].v] = u; u = E[e].v; } else { if(--cnt[dis[u]] == 0) break; for(minv = nv, e = H[u]; ~e; e = E[e].next) if(E[e].c > eps && minv > dis[E[e].v]) { minv = dis[E[e].v]; cur[u] = e; } dis[u] = minv + 1; cnt[dis[u]]++; u = pre[u]; } } return flow; } void init() { cntE = 0; memset(H, -1, sizeof H); } vector<int> vec; int a[maxn]; int b[maxn]; int color[maxn]; double c[maxn]; int n, m; bool check(double x) { double ans = 0; init(); s = 0, t = n + 1, nv = t + 1; for(int i = 1; i <= m; i++) { double t = c[i] - x; if(t > 0) { addedges(a[i], b[i], t); addedges(b[i], a[i], t); } else ans += t; } addedges(s, 1, INF); addedges(n, t, INF); ans += isap(); return ans > 0; } void dfs(int u) { if(color[u]) return; color[u] = 1; for(int e = H[u]; ~e; e = E[e].next) if(E[e].c > eps) { int v = E[e].v; dfs(v); } } void work() { for(int i = 1; i <= m; i++) scanf("%d%d%lf", &a[i], &b[i], &c[i]); double l = 0, r = INF, res = 0; while(r - l > eps) { double mid = (l + r) / 2; if(check(mid)) res = l = mid; else r = mid; } check(res); memset(color, 0, sizeof color); dfs(s); vec.clear(); for(int i = 1; i <= m; i++) { double t = c[i] - res; if(t < eps) vec.push_back(i); else if(color[a[i]] ^ color[b[i]]) vec.push_back(i); } printf("%d\n", (int)vec.size()); for(int i = 0; i < vec.size(); i++) printf("%d%c", vec[i], i == vec.size() - 1 ? '\n' : ' '); } int main() { while(scanf("%d%d", &n, &m) != EOF) { work(); } return 0; }