题目链接

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. 在leangoo 里怎么设置工作量估算,添加附件,设置截止时间?
  2. Python 之 Matplotlib (二)figure
  3. 计算机CAI应用实例,运用CAI课件辅助实验的实例分析
  4. 比世界纪录快3倍 阿里云377秒完成100TB数据排序
  5. YOLOv5 报错:“NotImplementedError: Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ back
  6. (2021年)IT技术分享社区个人文章汇总(数据库篇)
  7. osg布告板技术(Billboard)
  8. 搭建一个服务器框架,进程间利用管道通信,线程处理数据
  9. 为什么使用close()关闭所打开文件
  10. C4D建模宝典R20笔记
  11. SHELLEXECUTEINFO 和 ShellExecuteEx的使用
  12. 十五年学不会英语的原因
  13. Bias and Debias in Recommender System: A Survey and Future Directions学习笔记
  14. ajax 点击下一页,ajax调用不会进入下一页
  15. 将汉字转化为拼音的js插件
  16. What is the Softmax Function?详解机器学习中的Softmax函数【小白菜可懂】
  17. trick2-mobilenetv1、mobilenetv2、mobilenetv3替换YOLO主干
  18. 钢管车架管材的分级介绍 (zz)
  19. APP地推的一些经验
  20. 计算机键盘gt,雷神(Thunderobot)911GT-Y1笔记本电脑键盘评测-ZOL中关村在线

热门文章

  1. MyEclipse-6.5注冊码生成器源代码
  2. Zend Studio 7.2 汉化包 及安装方法
  3. lambda函数if_Python3中lambda表达式与函数式编程讲解
  4. 如何用PPT来实现三维3D效果,附参数设置详解
  5. matlab背景点状,基于MATLAB的点状目标检测
  6. 计算机技师工作调研,技师学院党委书记王庆余到计算机工程系进行“不忘初心、牢记使命”主题教育调研工作...
  7. elasticsearch 条件去重_Elasticsearch学习之查询去重
  8. 树莓派超声波车牌识别系统
  9. 中移4G模块-ML302-OpenCpu开发-串口开发
  10. mysql记录是乱码_mysql查询数据库导致中文乱码