Network Wars

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 2676
64-bit integer IO format: %lld      Java class name: Main

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

解题:最大流-分数规划?不懂。。。好厉害的样子啊
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const double exps = 1e-;
struct arc{
int to,next,id;
double flow;
arc(int x = ,double y = ,int z = ,int nxt = -){
to = x;
flow = y;
id = z;
next = nxt;
}
};
arc e[];
int head[maxn],d[maxn],cur[maxn],xx[maxn<<],yy[maxn<<];
int tot,S,T,n,m;
double ww[maxn<<];
bool used[maxn<<],vis[maxn<<];
void add(int u,int v,double flow,int id){
e[tot] = arc(v,flow,id,head[u]);
head[u] = tot++;
e[tot] = arc(u,,id,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof(d));
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > exps && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > exps && d[e[i].to] == d[u] + && (a = dfs(e[i].to,min(low,e[i].flow))) > exps){
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(low < exps) break;
}
}
if(tmp < exps) d[u] = -;
return tmp;
}
double dinic(){
double tmp = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
tmp += dfs(S,INF);
}
return tmp;
}
double init(double delta){
double tmp = tot = ;
memset(head,-,sizeof(head));
memset(used,false,sizeof(used));
for(int i = ; i <= m; ++i){
if(ww[i] - delta <= exps){
used[i] = true;
tmp += ww[i] - delta;
}else{
add(xx[i],yy[i],ww[i]-delta,i);
add(yy[i],xx[i],ww[i]-delta,i);
}
}
return tmp;
}
void dfs2(int u){
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(e[i].flow > exps && !vis[e[i].to]) dfs2(e[i].to);
}
int main(){
bool flag = false;
while(~scanf("%d %d",&n,&m)){
S = ;
T = n;
if(flag) puts("");
flag = true;
for(int i = ; i <= m; ++i) scanf("%d %d %lf",xx+i,yy+i,ww+i);
double mid,low = ,high = INF,ans = ;
while(fabs(high - low) > 1e-){
mid = (low + high)/2.0;
ans = init(mid);
ans += dinic();
if(ans > ) low = mid;
else high = mid;
}
memset(vis,false,sizeof(vis));
dfs2(S);
vector<int>res;
for(int i = ; i < n; ++i)
for(int j = head[i]; ~j; j = e[j].next)
if(vis[i] != vis[e[j].to]) used[e[j].id] = true;
for(int i = ; i <= m; ++i)
if(used[i]) res.push_back(i);
printf("%d\n",res.size());
for(int i = ; i < res.size(); ++i)
printf("%d%c",res[i],i == res.size()-?'\n':' ');
}
return ;
}

最新文章

  1. 浅析jquery ui的datepicker组件
  2. 【分享】纯js的n级联动列表框 —— 基于jQuery,支持下拉列表框和列表框,最重要的是n级,当然还有更重要的
  3. Java JDBC批处理插入数据操作
  4. delphi的几个特别关键字 object absolute
  5. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(5)
  6. Android(java)学习笔记233: 远程服务的应用场景(移动支付案例)
  7. python变量字符拼接
  8. 关于A2C算法
  9. 微信小程序:将中文语音直接转化成英文语音
  10. php(三)使用thinkphp操作数据库
  11. JVM的类加载
  12. .net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(二)
  13. 什么是PLI?
  14. 教你怎么看网站是用react搭建的
  15. 2015 HIAST Collegiate Programming Contest H
  16. Cookie浅谈
  17. 极简 R 包建立方法--转载
  18. [poj1269]Intersecting Lines
  19. linux 16进制 产看文件
  20. stm32 堆和栈(stm32 Heap &amp; Stack)

热门文章

  1. SQLPlus在连接时通常有四种方式
  2. linux 下 The valid characters are defined in RFC 7230 and RFC 3986
  3. 打造一个全命令行的Android构建系统
  4. Cocos2dx之使用UI库结合cocostudio
  5. 【解决】run-as: Package &amp;#39;&amp;#39; is unknown
  6. js面向对象编程:怎样实现方法重载
  7. c++ std
  8. SQLSERVER 链接服务器
  9. tp5数据库操作 Db类
  10. com.sun.jdi.internalException:Unexpected JDWP Error:103////Method threw &#39;java.lang.IllegalArgumentEx