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

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

Output

4

3 4 5 6

3

1 2 3

Source: Andrew Stankevich’s Contest #8

在Amber写的《最小割在信息学竞赛中的应用》看到的一道例题,所以就拿来做做,但是出现了不少的问题

题意:给出一个带权的无向图,每一条边有一个权值w,求将s与t分开的一个边割集,使得边割集的平均值最小。

具体的做法可以看看AMber的论文,这里有几个疑惑

1. 为什么在DFS过程中不加引用就TLE

2. 为什么在Dinic过程中不复制就会WA

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream> using namespace std; const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int MaxN = 110; const int MaxM = 51000; typedef struct Node
{
int u,v,cap;
}Point ; typedef struct node
{
int v,next; double cap;
}edge; Point a[MaxN*4]; edge e[MaxM]; int H[MaxN],Head[MaxN],top,vis[MaxN]; int n,m; double L,R; int dbcmp(double s)
{
if(fabs(s)<eps)
{
return 0;
} return s>0?1:-1;
} void AddEdge(int u,int v,double cap)
{
e[top].v =v ;e[top].cap = cap; e[top].next = H[u]; H[u] = top++;
} bool BFS()
{
memset(vis,0,sizeof(vis)); vis[1] =1; queue<int>Q; Q.push(1); while(!Q.empty())
{
int u =Q.front(); Q.pop(); for(int i = H[u];~i;i = e[i].next)
{
if(dbcmp(e[i].cap)>0&&!vis[e[i].v])
{
vis[e[i].v] = vis[u]+1; Q.push(e[i].v); if(e[i].v==n)
{
return 1;
}
}
}
} return 0;
} double DFS(int u,double cap)
{
if(u==n)
{
return cap;
}
double ans =0; for(int &i =Head[u];i!=-1; i = e[i].next) //不加引用就超时
{
if(vis[e[i].v]==vis[u]+1&&dbcmp(e[i].cap)>0)
{
double ant = DFS(e[i].v,min(cap,e[i].cap)); if(ant)
{
e[i].cap-=ant; e[i^1].cap+=ant; return ant;
}
}
}
return 0;
} double Dinic()//求最小割
{
double ans = 0; while(BFS())
{
memcpy(Head,H,sizeof(H));//不复制就WA
while(double ant = DFS(1,INF))
ans+=ant;
}
return ans;
} double Build(double s)
{
top =0; memset(H,-1,sizeof(H)); double ans = 0; for(int i=1;i<=m;i++)
{
if(a[i].cap>s)
{
AddEdge(a[i].u,a[i].v,a[i].cap-s); AddEdge(a[i].v,a[i].u,a[i].cap-s);
}
else ans += (a[i].cap-s);
} return ans+Dinic();
} double Search()
{
double mid; while(dbcmp(R-L)>0)
{
mid = (L+R)/2; double ant = Build(mid); if(dbcmp(ant)>0)
{ L = mid;
}
else
{
R = mid;
}
}
return mid;
} void dfs(int u)
{
vis[u] = 1; for(int i = H[u];i!=-1;i = e[i].next)
{
if(dbcmp(e[i].cap)>0&&!vis[e[i].v])
{
dfs(e[i].v);
}
}
} int main()
{
int z = 0; while(~scanf("%d %d",&n,&m))
{
L = 0,R = 0;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].cap); R+=a[i].cap;
} double ans = Search(); Build(ans); memset(vis,0,sizeof(vis)); dfs(1); int num = 0; for(int i=1;i<=m;i++)
{
if(vis[a[i].v]+vis[a[i].u]==1||a[i].cap<ans)
{
num++;
}
} if(z++)
{
printf("\n");
} printf("%d\n",num); bool flag = false; for(int i=1;i<=m;i++)
{
if(vis[a[i].v]+vis[a[i].u]==1||a[i].cap<ans)
{
if(flag)
{
printf(" ");
}
else flag= true; printf("%d",i);
}
}
printf("\n");
}
return 0;
}

Network Wars-ZOJ2676最小割+01规划的更多相关文章

  1. POJ 1966 Cable TV Network 【经典最小割问题】

    Description n个点的无向图,问最少删掉几个点,使得图不连通 n<=50 m也许可以到完全图? Solution 最少,割点,不连通,可以想到最小割. 发现,图不连通,必然存在两个点不 ...

  2. CodeChef - RIN 最小割应用 规划问题

    题意:给定\(n\)门课和\(m\)个学期,每门课在每个学期有不同的得分,需要选定一个学期去完成,但存在约束条件,共有\(k\)对课程需要\(a\)在\(b\)开始学前学会,求最大得分(原问题是求最高 ...

  3. UVA1660 电视网络 Cable TV Network[拆点+最小割]

    题意翻译 题目大意: 给定一个n(n <= 50)个点的无向图,求它的点联通度.即最少删除多少个点,使得图不连通. 解析 网络瘤拆点最小割. 定理 最大流\(=\)最小割 感性地理解(口胡)一下 ...

  4. POJ 1966 Cable TV Network (点连通度)【最小割】

    <题目链接> 题目大意: 给定一个无向图,求点连通度,即最少去掉多少个点使得图不连通. 解题分析: 解决点连通度和边连通度的一类方法总结见   >>> 本题是求点连通度, ...

  5. ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)

    [题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...

  6. zoj 2676 Network Wars 0-1分数规划+最小割

    题目详解出自 论文 Amber-最小割模型在信息学竞赛中的应用 题目大意: 给出一个带权无向图 G = (V,E), 每条边 e属于E都有一个权值We,求一个割边集C,使得该割边集的平均边权最小,即最 ...

  7. HDU 2676 Network Wars 01分数规划,最小割 难度:4

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use ...

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

    Network Wars 07年胡伯涛的论文上的题:http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html 代码: #include < ...

  9. zoj 2676 二分+ISAP模板求实型参数的最小割(0-1分数规划问题)(可做ISAP模板)

    /* 参考博文:http://www.cnblogs.com/ylfdrib/archive/2010/09/01/1814478.html 以下题解为转载代码自己写的: zoj2676 胡伯涛论文& ...

随机推荐

  1. 解决mysql数据库插入中文字段时出现??? 的问题

    1.检查并修改mysql的my.ini的配置文件   default-character-set=utf8 2.建立数据库是要指定字符集   create database mydb default ...

  2. win7 将所有 视图 改为 '详细信息'

    1.随便进入某个文件夹->(菜单栏中)查看->选'详细信息' 2.(菜单栏中)工具->文件夹选项->查看->'应用到文件夹'

  3. Spring IoC容器初始化过程学习

    IoC容器是什么?IoC文英全称Inversion of Control,即控制反转,我么可以这么理解IoC容器: 把某些业务对象的的控制权交给一个平台或者框架来同一管理,这个同一管理的平台可以称为I ...

  4. 简易版C语言程序设计语法

    源程序 → 外部声明 | 子程序(外部声明) 外部声明   → 函数定义| 函数声明 函数定义 → 类型标识符(复合句) 标识符类型 → 无类型 | 字符型 | 整型 | 浮点型 整型→ 长整型 | ...

  5. winform 获取当前程序运行根目录

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  6. 毕业设计 之 二 PHP学习笔记(一)

    毕业设计 之 二 PHP学习笔记(一) 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 一.环境搭建 1.XAMPP下载安装 XAMPP是PHP.MySQL ...

  7. JMeter学习-035-JMeter调试工具之二---Debug PostProcessor

    前文 JMeter学习-034-JMeter调试工具之一---HTTP Mirror Server讲述了HTTP镜像服务器在调试请求入参时的实例应用.此文我们讲述另一种测试脚本调试工具的使用. 前置处 ...

  8. Windows zabbix监控远程进程实现机制

    最近负责zabbix监控部署方面的工作,需要完成本地服务端监控远程虚拟机的运行状态(CPU.打开的进程等),与大家分享下我的实现方法. (1) 首先,需要实现记录zabbix客户端的进程的批处理:za ...

  9. docker好文收藏

    深入浅出Docker(一):Docker核心技术预览 2. 核心技术预览 Docker核心是一个操作系统级虚拟化方法, 理解起来可能并不像VM那样直观.我们从虚拟化方法的四个方面:隔离性.可配额/可度 ...

  10. CLI:使用Go开发命令行应用

      原文地址 CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如 ...