【01分数规划】 ZOJ 2676 Network Wars
2015-05-05 19:34
330 查看
点击打开链接
题意:取一个边集,使得拦截1->n的流量,使得边集的平均权值最低
分数规划:
题意:取一个边集,使得拦截1->n的流量,使得边集的平均权值最低
分数规划:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> using namespace std; #define ll long long #pragma comment(linker, "/STACK:102400000,102400000") #define mod (ll)4294967296 const int MAXN = 51222;//点数的最大值 const int MAXM = 22222;//边数的最大值 const int INF = 0x3f3f3f3f; const double eps = 1e-4; struct Edge { int to,next; double cap,flow; }edge[MAXM];//注意是MAXM int tol; int head[MAXN]; int gap[MAXN],dep[MAXN],cur[MAXN]; void init() { tol = 0; memset(head,-1,sizeof (head)); } void addedge (int u,int v,double w,double rw = 0) { edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = w; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } int Q[MAXN]; void BFS(int start,int end) { memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0] = 1; int front = 0, rear = 0; dep[end] = 0; Q[rear++] = end; while(front != rear) { int u = Q[front++]; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i]. to; if(dep[v] != -1)continue; if(edge[i^1].cap-edge[i^1].flow<=eps) continue;//end开始所以取反向边 Q[rear++] = v; dep[v] = dep[u] + 1; gap[dep[v]]++; } } return ; } int S[MAXN]; double sap(int start,int end, int N) { BFS(start,end); memcpy(cur,head,sizeof(head)); int top = 0; int u = start; double ans = 0; int i; while(dep[start] < N) { if(u == end) { double Min = INF*1.0; int inser; for( i = 0;i < top;i++) { if(Min > edge[S[i]].cap - edge[S[i]].flow) { Min = edge[S[i]].cap - edge[S[i]].flow; inser = i; } } for( i = 0;i < top;i++) { edge[S[i]]. flow += Min; edge[S[i]^1].flow -= Min; } ans += Min; top = inser; u = edge[S[top]^1].to; continue; } bool flag = false; int v; for( i = cur[u]; i != -1; i = edge[i]. next) { v = edge[i]. to; if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]) { flag = true; cur[u] = i; break; } } if(flag) { S[top++] = cur[u]; u = v; continue; } int Min = N; for( i = head[u]; i != -1; i = edge[i].next) { if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) { Min = dep[edge[i].to]; cur[u] = i; } } gap[dep[u]]--; if(!gap[dep[u]]) return ans; dep[u] = Min + 1; gap[dep[u]]++; if(u != start)u = edge[S[--top]^1].to; } return ans; } int a[MAXN],b[MAXN]; double c[MAXN]; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { init(); double l=INF*1.0,r=0; for(int i=1;i<=m;i++) { scanf("%d%d%lf",&a[i],&b[i],&c[i]); l=min(l,c[i]),r=max(r,c[i]); } while(r-l>eps) { init(); double lr=(l+r)/2.0,flow=0; for(int i=1;i<=m;i++) { if(c[i]<=lr) flow+=c[i]-lr; else addedge(a[i],b[i],c[i]-lr,0); } flow+=sap(1,n,n); if(flow>0) l=lr; else r=lr; } init(); for(int i=1;i<=m;i++) if(c[i]>r) addedge(a[i],b[i],c[i]-r,0); else addedge(0,0,0,0); sap(1,n,n); BFS(1,n); vector<int>ans; for(int i=1;i<=m;i++) { if(c[i]<=r||((dep[a[i]]==-1)^(dep[b[i]]==-1))) ans.push_back(i); } printf("%d\n",ans.size()); for(int i=0;i<ans.size();++i) { if(i>0) printf(" "); printf("%d",ans[i]); } printf("\n"); } return 0; } /* 6 8 1 2 3 1 3 3 2 4 2 2 5 2 3 4 2 3 5 2 5 6 3 4 6 3 4 5 1 2 2 1 3 2 2 3 1 2 4 2 3 4 2 */
相关文章推荐
- 【ZOJ】2676 Network Wars 01分数规划+最小割
- ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)
- ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)
- ZOJ 2676 Network Wars(01分数规划-二分+最小割)
- zoj 2676 Network Wars(01分数规划+最大流)
- zoj 2676 Network Wars(01分数规划+网络流)
- zoj 2676 Network Wars(01分数规划+最小割)
- zoj 2676 Network Wars(最小割,01分数规划)
- ZOJ 2676 Network Wars[01分数规划]
- 【ZOJ 2676】Network Wars 网络战争 网络流 01分数规划
- zoj 2676 Network Wars 最小割+0-1分数规划
- zoj 2676 Network Wars 【0-1分数规划 + 最小割】 【吃一堑长一智】
- ZOJ 2676 01分数规划 最小割
- HDU 2676 Network Wars 01分数规划,最小割 难度:4
- zoj 2676 Network Wars 0-1分数规划+最小割
- zoj 2676 网络流+01分数规划
- ZOJ-2676-Network Wars(01分数规划+最小割)
- ZOJ 2676 Network Wars 最小割 分数规划
- zoj2676 Network Wars (01分数规划)
- [ZJU 2676]Network Wars(分数规划+最小割)