题目链接

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 network are especially important --- they are connected to global world network and president palace network respectively.

The server connected to the president palace network has number 1, and the server connected to the global world network has number n.

Recently the company Max Traffic has decided to take control over some cables so that it could see what data is transmitted by the president palace users. Of course they want to control such set of cables, that it is impossible to download any data from the global network to the president palace without transmitting it over at least one of the cables from the set.

To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has some cost. Since the company's main business is not spying, but providing internet connection to home users, its management wants to make the operation a good investment. So it wants to buy such a set of cables, that cables mean cost} is minimal possible.

That is, if the company buys k cables of the total cost c, it wants to minimize the value of c/k.

Input

There are several test cases in the input. The first line of each case contains n and m (2 <= n <= 100 , 1 <= m <= 400 ). Next m lines describe cables~--- each cable is described with three integer numbers: servers it connects and the cost of the cable. Cost of each cable is positive and does not exceed 107.

Any two servers are connected by at most one cable. No cable connects a server to itself. The network is guaranteed to be connected, it is possible to transmit data from any server to any other one.

There is an empty line between each cases.

Output

First output k --- the number of cables to buy. After that output the cables to buy themselves. Cables are numbered starting from one in order they are given in the input file. There should an empty line between each cases.

Example

Input Output
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
3 4 5 6
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2
3
1 2 3

Source: Andrew Stankevich's Contest #8

Submit    Status

题意:

题解:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=1e9;
const ll mod=1000000007;
const double eps=1e-8;  //

const int maxn=210;//点数的最大值
const int maxm=20500;//边数的最大值
struct Node
{int from,to,next;double cap;
}edge[maxm];struct nod
{int u,v,w;nod(int a=0,int b=0,int c=0):u(a),v(b),w(c) {}
}e[550];int tol;
int dep[maxn];//dep为点的层次
int head[maxn];
int n,m;
void init()
{tol=0;memset(head,-1,sizeof(head));
}
void addedge(int u,int v,double w)//第一条边下标必须为偶数
{edge[tol].from=u;edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;edge[tol].from=v;edge[tol].to=u;edge[tol].cap=0;edge[tol].next=head[v];head[v]=tol++;
}
int BFS(int start,int end)
{int que[maxn];int front,rear;front=rear=0;memset(dep,-1,sizeof(dep));que[rear++]=start;dep[start]=0;while(front!=rear){int u=que[front++];if(front==maxn)front=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(edge[i].cap>0&&dep[v]==-1){dep[v]=dep[u]+1;que[rear++]=v;if(rear>=maxn)rear=0;if(v==end)return 1;}}}return 0;
}
double dinic(int start,int end)
{double res=0;int top;int stack[maxn];//stack为栈,存储当前增广路int cur[maxn];//存储当前点的后继while(BFS(start,end)){memcpy(cur,head,sizeof(head));int u=start;top=0;while(1){if(u==end){double min=inf;int loc;for(int i=0;i<top;i++)if(min>edge[stack[i]].cap){min=edge[stack[i]].cap;loc=i;}for(int i=0;i<top;i++){edge[stack[i]].cap-=min;edge[stack[i]^1].cap+=min;}res+=min;top=loc;u=edge[stack[top]].from;}for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])break;if(cur[u]!=-1){stack[top++]=cur[u];u=edge[cur[u]].to;}else{if(top==0)break;dep[u]=-1;u=edge[stack[--top]].from;}}}return res;
}double check(double x)
{double ans=0;tol=0;rep(i,1,n+1) head[i]=-1;rep(i,1,m+1){if(e[i].w-x<0){ans+=e[i].w-x;continue;}addedge(e[i].u,e[i].v,e[i].w-x);addedge(e[i].v,e[i].u,e[i].w-x);}int st=1,ed=n;ans+=dinic(st,ed);return ans;
}int vis[maxn];
queue<int> q;
void bfs()
{while(!q.empty()) q.pop();vis[1]=1;q.push(1);while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(fabs(edge[i].cap)<eps) continue;if(vis[v]) continue;vis[v]=1;q.push(v);}}
}
set<int> sel;
int main()
{while(~scanf("%d%d",&n,&m)){rep(i,1,n+1) vis[i]=0;sel.clear();ll sum=0;int mi=inf;rep(i,1,m+1){scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);sum+=1ll*e[i].w;mi=min(mi,e[i].w);}double l=1.0*mi/n,r=1.0*sum;double ans=0;while(1){double m=(l+r)/2;double res=check(m);if(fabs(res)<eps){ans=m;bfs();break;}if(res>0) l=m;else r=m;}rep(i,1,m+1){if(vis[e[i].u]+vis[e[i].v]==1)sel.insert(i);if(e[i].w<l)sel.insert(i);}printf("%d\n",(int)sel.size());set<int>::iterator it;int cnt=0;for(it=sel.begin();it!=sel.end();it++){cnt++;printf("%d%c",*it,cnt==sel.size()? '\n':' ');}}return 0;
}

转载于:https://www.cnblogs.com/tarjan/p/7273452.html

zoj 1676Network Wars(胡博涛论文题,01分数规划+最小割)相关推荐

  1. ZOJ - 2676 Network Wars(01分数规划+最小割)

    题目链接:点击查看 题目大意:给出一个 n 个点和 m 条边组成的无向带权图,现在需要求一个将点 1 和点 n 分开的割集 C ,使得 最小 题目分析:分数式为总权值比上边的数量,换句话说就是一条边只 ...

  2. poj2976 Dropping tests(01分数规划 好题)

    https://vjudge.net/problem/POJ-2976 又是一波c++AC,g++WA的题.. 先推导公式:由题意得 Σa[i]/Σb[i]<=x,二分求最大x.化简为Σ(a[i ...

  3. Loj #149. 01 分数规划(01分数规划模板题)

    链接 题意: 题解: 详细解法看这里 这里说个点,eps一定要开足够小,我一开始开的1e-5,结果就过了90%的数据,开到1e-7就足够了 代码: #include<bits/stdc++.h& ...

  4. 【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

    题干: In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your ...

  5. 2018牛客暑假多校A题GPA 01分数规划

    链接:https://www.nowcoder.com/acm/contest/143/A 来源:牛客网 gpa 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语 ...

  6. 北大直博保送生论文涉嫌抄袭?原作者实名举报,北大南开火速调查

    [导读]保送北大直博的学生被扒出抄袭了川大学生的SCI论文,还是从论文机构买的? 南开保送北大直博的学生,抄袭川大学生的SCI论文发了本普刊,还是直接英翻中? 更为离奇的是,这篇抄袭论文似乎是从论文辅 ...

  7. POJ-2234 Matches Game---尼姆博奕裸题

    题目链接: https://vjudge.net/problem/POJ-2234 题目大意: 尼姆博奕裸题 思路: 直接异或 1 #include<iostream> 2 #includ ...

  8. 西交大计算机考博学术英语,2018年西安交通大学考博英语真题

    "2018年西安交通大学考博英语真题"小编正在努力更新中,请关注希赛网英语考试频道,以下为考博英语预测题库精选试题. Part IV Short Essay Writing (20 ...

  9. c语言考博真题,中国科学院2015年考博英语真题及答案

    希赛网考博英语为广大考生整理出中国科学院2014年考博英语真题及答案,查看文末,下载考博英语PDF(完整版).希赛网考博英语真题持续更新中,请关注希赛网考博英语频道. Part I Vocabular ...

最新文章

  1. ARM Cortex-M嵌入式C基础编程(下)
  2. SAP EWM - 其他主数据 - 承运方
  3. html css文本框按钮,css样式之区分input是按钮还是文本框的方法
  4. 为什么SD-WAN5年增长超过40%,越来越受企业欢迎?
  5. 线上分享 | 产品架构搭建:从业务到体系
  6. 关于最近使用文档的几个技巧
  7. spring-boot注解详解(一)
  8. ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
  9. 东北电力大学计算机专业几本,东北电力大学是几本?东北电力大学怎么样?
  10. java 折线图 放大 缩小_可拖拉放大缩小HC折线图 | JShare
  11. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】
  12. 花生采摘(洛谷-P1086)
  13. go get如何删除_在Go使用Sqlite和Accsee
  14. 基于JAVA+SpringBoot+Vue+Mybatis+MYSQL的微信小程序点餐系统
  15. Kotlin-Note
  16. 探索ASP.NET MVC5系列之~~~1.基础篇---必须知道的小技能
  17. SpringBoot入门篇-简介
  18. Thymeleaf数据回显
  19. 给惠普735g5 装Win10+Ubuntu 16.04双系统
  20. c++ socket下ipv4到ipv6的移植

热门文章

  1. 用fuser或者lsof解决无法umount问题(device is busy)
  2. Open vswitch 之Qos rate-limiting 原理
  3. 【Java】_2_Java程序入门第五课
  4. 通过组织发展来推动组织变革
  5. xps15u盘装linux,Dell XPS 15 9560 安装 Ubuntu 18.04
  6. python常量列表_Python中实现常量(Const)功能
  7. 查询mysql 中的空文本_MySQL查询以显示空列的自定义文本
  8. PostgreSQL 12系统表(3)pg_tablespace
  9. java算法题走楼梯,程序员必学算法「动态规划」:爬楼梯(完全背包解法)
  10. 哪个html元素指定了页面描述,网页的设计HTML元素属性2.doc