ASC7 Problem G. Network Wars

题目大意

给你一个$n$个点$m$条带权双向边的图,求选取割的集合,最小化$$\frac{\sum_{i\in cut}c_i}{|cut|}$$

简要题解

01分数规划,先二分答案,然后把边权设为$c[i]-ans$,如果这个值小于0,显然要选这个边,再加上最小割的值,如果这个和小于0,则说明二分的答案太大,否则太小。

最后输出结果,方法是先bfs得到S集合,然后横跨S和T的边在割中。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 namespace my_header {
  4 #define pb push_back
  5 #define mp make_pair
  6 #define pir pair<int, int>
  7 #define vec vector<int>
  8 #define pc putchar
  9 #define clr(t) memset(t, 0, sizeof t)
 10 #define pse(t, v) memset(t, v, sizeof t)
 11 #define bl puts("")
 12 #define wn(x) wr(x), bl
 13 #define ws(x) wr(x), pc(‘ ‘)
 14     typedef long long LL;
 15     typedef double DB;
 16     inline char gchar() {
 17         char ret = getchar();
 18         for(; (ret == ‘\n‘ || ret == ‘\r‘ || ret == ‘ ‘) && ret != EOF; ret = getchar());
 19         return ret; }
 20     template<class T> inline void fr(T &ret, char c = ‘ ‘, int flg = 1) {
 21         for(c = getchar(); (c < ‘0‘ || ‘9‘ < c) && c != ‘-‘; c = getchar());
 22         if (c == ‘-‘) { flg = -1; c = getchar(); }
 23         for(ret = 0; ‘0‘ <= c && c <= ‘9‘; c = getchar())
 24             ret = ret * 10 + c - ‘0‘;
 25         ret = ret * flg; }
 26     inline int fr() { int t; fr(t); return t; }
 27     template<class T> inline void fr(T&a, T&b) { fr(a), fr(b); }
 28     template<class T> inline void fr(T&a, T&b, T&c) { fr(a), fr(b), fr(c); }
 29     template<class T> inline char wr(T a, int b = 10, bool p = 1) {
 30         return a < 0 ? pc(‘-‘), wr(-a, b, 0) : (a == 0 ? (p ? pc(‘0‘) : p) :
 31             (wr(a/b, b, 0), pc(‘0‘ + a % b)));
 32     }
 33     template<class T> inline void wt(T a) { wn(a); }
 34     template<class T> inline void wt(T a, T b) { ws(a), wn(b); }
 35     template<class T> inline void wt(T a, T b, T c) { ws(a), ws(b), wn(c); }
 36     template<class T> inline void wt(T a, T b, T c, T d) { ws(a), ws(b), ws(c), wn(d); }
 37     template<class T> inline T gcd(T a, T b) {
 38         return b == 0 ? a : gcd(b, a % b); }
 39     template<class T> inline T fpw(T b, T i, T _m, T r = 1) {
 40         for(; i; i >>= 1, b = b * b % _m)
 41             if(i & 1) r = r * b % _m;
 42         return r; }
 43 };
 44 using namespace my_header;
 45
 46 static const int maxE = 200000, maxV = 2000 + 100;
 47 static const double EPS = 1e-6;
 48 static const double INF = 2e8;
 49 struct MaxFlow {
 50     int e, h[maxV], to[maxE], nxt[maxE];
 51     double cap[maxE];
 52     void init() {
 53         memset(h, -1, sizeof h);
 54         e = 0;
 55     }
 56     void addEdge(int u,int v, double c) {
 57         to[e] = v, nxt[e] = h[u], cap[e] = c, h[u] = e++;
 58         to[e] = u, nxt[e] = h[v], cap[e] = c, h[v] = e++;
 59     }
 60
 61     int dis[maxV], que[maxE * 20], qh, qt;
 62     bool inq[maxV];
 63     int s, t;
 64
 65     int sgn(double x) {
 66         return x < -EPS ? -1 : EPS < x;
 67     }
 68
 69     bool bfs() {
 70         que[qh = qt = 0] = s;
 71         memset(inq, 0, sizeof inq);
 72         memset(dis, 0x3f, sizeof dis);
 73         dis[s] = 0;
 74         inq[s] = 1;
 75         while (qh <= qt) {
 76             int u = que[qh++];
 77             for (int i = h[u]; i != -1; i = nxt[i])
 78                 if (cap[i] > EPS && !inq[to[i]]) {
 79                     int v = to[i];
 80                     inq[v] = 1;
 81                     dis[v] = dis[u] + 1;
 82                     que[++qt] = v;
 83                 }
 84         }
 85         return dis[t] != 0x3f3f3f3f;
 86     }
 87
 88     double dfs(int u, double a = 0) {
 89         if (u == t || sgn(a) == 0)
 90             return a;
 91         double flow = 0, f;
 92         for (int i = h[u]; i != -1; i = nxt[i])
 93             if (dis[to[i]] == dis[u]+1 && (f = dfs(to[i], min(a, cap[i]))) > 0) {
 94                 flow += f;
 95                 cap[i] -= f;
 96                 cap[i^1] += f;
 97                 a -= f;
 98                 //if (a == 0) break;
 99                 if (a == 0) {
100                     return flow;
101                 }
102             }
103         dis[u] = -1;
104         // 原来我写了两年的网络流算法都是错的 = = 膜拜wmj大爷
105         return flow;
106     }
107
108     double maxFlow() {
109         double flow = 0;
110         while (bfs())
111             flow += dfs(s, INF);
112         return flow;
113     }
114 } mf;
115
116 const int MAXM = 500;
117 int n, m;
118 int u[MAXM], v[MAXM], w[MAXM];
119
120
121 int main() {
122 #ifdef lol
123     freopen("G.in", "r", stdin);
124     freopen("G.out", "w", stdout);
125 #else
126     freopen("network.in", "r", stdin);
127     freopen("network.out", "w", stdout);
128 #endif
129     fr(n, m);
130     for (int i = 1; i <= m; ++i)
131         fr(u[i], v[i], w[i]);
132     double l = 0, r = 2e7, c;
133     while (r - l > 1e-7) {
134         c = (l + r) / 2;
135         mf.init();
136         mf.s = 1, mf.t = n;
137         double t = 0;
138         for (int i = 1; i <= m; ++i)
139             if (w[i] - c > EPS)
140                 mf.addEdge(u[i], v[i], w[i] - c);
141             else if (w[i] - c < -EPS)
142                 t += w[i] - c;
143         t += mf.maxFlow();
144         if (t < EPS)
145             r = c;
146         else l = c;
147     }
148     c = (l + r) / 2;
149     vector<int> ans;
150     for (int i = 1; i <= m; ++i) {
151         if (w[i] - c < EPS || (mf.inq[u[i]] ^ mf.inq[v[i]]))
152             ans.pb(i);
153     }
154     wt(ans.size());
155     for (auto &&k : ans)
156         ws(k);
157     bl;
158
159     return 0;
160 }
时间: 2024-07-30 10:04:30

ASC7 Problem G. Network Wars的相关文章

ZOJ 2676 Network Wars[01分数规划]

ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both direction

ZJU 2676 Network Wars

Network Wars Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 267664-bit integer IO format: %lld      Java class name: Main Special Judge Network of Byteland consists of n servers, connected by m optical cable

ZOJ 2676 Network Wars(最优比例最小割)

Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the n

Problem G 宝石合成 (内蒙古14年省赛)

1117: Problem G 宝石合成 时间限制: 1 Sec  内存限制: 128 MB 提交: 18  解决: 4 [提交][状态][讨论版] 题目描述 故事人物:豆豆强 走上致富之路的豆豆强越来越有土豪范,买房买车已经提不起他的兴趣,现在玩起了魔法宝石. 这种宝石分为不同品质等级,并且3个品质等级为 i 的宝石,可以合成一个品质等级为 i+1 的宝石.更加神奇的是,不管这个宝石的品质怎么变,一个宝石永远只占一个单位的空间. 现在豆豆强需要买一个全金打造的保险柜来储存这些宝石.本来对于他这

zoj 2676 Network Wars(最小割,01分数规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2676 大致题意:给出一个带权无向图,每条边有一个边权wi,求将S和T分开的一个割边集C,使得该割边集的平均边权最小,即最小化∑wi / |C| . 详见amber关于最小割模型的论文 思路:amber论文中详细讲解了如何转化成函数及建图,值得注意的是当边被重新赋权后,对于wi < 0 的边权,该边必然在最小割中,不必再建边,直接加入最大流中即可,因为求最小割时边权都为正值

实验12:Problem G: 强悍的矩阵运算来了

这个题目主要是乘法运算符的重载,卡了我好久,矩阵的乘法用3个嵌套的for循环进行,要分清楚矩阵的乘法结果是第一个矩阵的行,第二个矩阵的列所组成的矩阵. 重载+,*运算符时,可以在参数列表中传两个矩阵引用,分别表示前后进行运算的矩阵,或者是只传运算符之后的矩阵引用,前一个矩阵用的是隐含的this指针指向的矩阵.我用的是后者. Home Web Board ProblemSet Standing Status Statistics Problem G: 强悍的矩阵运算来了 Problem G: 强悍

zoj2676 Network Wars(0-1分数规划,最大流模板)

Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include <algorithm> #include <cstdio> #include <iterator> #include <limits> #include <vector> #include <string.h> const int N = 11

2018 Multi-University Training Contest 3 1007 / hdu6325 Problem G. Interstellar Travel 凸包

Problem G. Interstellar Travel 题意: 给定平面上n个点,起点1 为(0,0),终点 n 为(Xn, 0),其它点的横坐标 0 <Xi<Xn,纵坐标 Xi >=0.每次可以飞到一个横坐标严格更大的点,代价为两个坐标的叉积.求起点到终点总代价最小的飞行路线,并输出字典序最小的路线.2≤n≤200000. Shortest judge solution: 979 bytes 题解: 显然坐标相同的点里只保留编号最小的点最优. 将起点到终点的路径补全为终点往下走到

GoldenGate OGG-01032 There Is a Problem in Network Communication Error in Writing to Rmt Remote Trail Rmttrail (Doc ID 1446621.1)

GoldenGate OGG-01032 There Is a Problem in Network Communication Error in Writing to Rmt Remote Trail Rmttrail (Doc ID 1446621.1) To Bottom In this Document   Symptoms   Changes   Cause   Solution APPLIES TO: Oracle GoldenGate - Version 10.0.0.0 to 1