Network Wars
This problem will be judged on ZJU. Original ID: 2676
64-bit integer IO format: %lld Java class name: Main
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 ;
}