首页 > 代码库 > ZJU 2676 Network Wars

ZJU 2676 Network Wars

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 81 2 31 3 32 4 22 5 23 4 23 5 25 6 34 6 3

4 51 2 21 3 22 3 12 4 23 4 2

Output
43 4 5 6 

31 2 3

Source

Andrew Stankevich‘s Contest #8

解题:最大流-分数规划?不懂。。。好厉害的样子啊

  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <climits>  7 #include <vector>  8 #include <queue>  9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 110; 18 const double exps = 1e-8; 19 struct arc{ 20     int to,next,id; 21     double flow; 22     arc(int x = 0,double y = 0,int z = 0,int nxt = -1){ 23         to = x; 24         flow = y; 25         id = z; 26         next = nxt; 27     } 28 }; 29 arc e[20000]; 30 int head[maxn],d[maxn],cur[maxn],xx[maxn<<2],yy[maxn<<2]; 31 int tot,S,T,n,m; 32 double ww[maxn<<2]; 33 bool used[maxn<<2],vis[maxn<<2]; 34 void add(int u,int v,double flow,int id){ 35     e[tot] = arc(v,flow,id,head[u]); 36     head[u] = tot++; 37     e[tot] = arc(u,0,id,head[v]); 38     head[v] = tot++; 39 } 40 bool bfs(){ 41     queue<int>q; 42     memset(d,-1,sizeof(d)); 43     d[S] = 1; 44     q.push(S); 45     while(!q.empty()){ 46         int u = q.front(); 47         q.pop(); 48         for(int i = head[u]; ~i; i = e[i].next){ 49             if(e[i].flow > exps && d[e[i].to] == -1){ 50                 d[e[i].to] = d[u] + 1; 51                 q.push(e[i].to); 52             } 53         } 54     } 55     return d[T] > -1; 56 } 57 double dfs(int u,double low){ 58     if(u == T) return low; 59     double tmp = 0,a; 60     for(int &i = cur[u]; ~i; i = e[i].next){ 61         if(e[i].flow > exps && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow))) > exps){ 62             e[i].flow -= a; 63             e[i^1].flow += a; 64             tmp += a; 65             low -= a; 66             if(low < exps) break; 67         } 68     } 69     if(tmp < exps) d[u] = -1; 70     return tmp; 71 } 72 double dinic(){ 73     double tmp = 0; 74     while(bfs()){ 75         memcpy(cur,head,sizeof(head)); 76         tmp += dfs(S,INF); 77     } 78     return tmp; 79 } 80 double init(double delta){ 81     double tmp = tot = 0; 82     memset(head,-1,sizeof(head)); 83     memset(used,false,sizeof(used)); 84     for(int i = 1; i <= m; ++i){ 85         if(ww[i] - delta <= exps){ 86             used[i] = true; 87             tmp += ww[i] - delta; 88         }else{ 89             add(xx[i],yy[i],ww[i]-delta,i); 90             add(yy[i],xx[i],ww[i]-delta,i); 91         } 92     } 93     return tmp; 94 } 95 void dfs2(int u){ 96     vis[u] = true; 97     for(int i = head[u]; ~i; i = e[i].next) 98         if(e[i].flow > exps && !vis[e[i].to]) dfs2(e[i].to); 99 }100 int main(){101     bool flag = false;102     while(~scanf("%d %d",&n,&m)){103         S = 1;104         T = n;105         if(flag) puts("");106         flag = true;107         for(int i = 1; i <= m; ++i) scanf("%d %d %lf",xx+i,yy+i,ww+i);108         double mid,low = 0,high = INF,ans = 0;109         while(fabs(high - low) > 1e-3){110             mid = (low + high)/2.0;111             ans = init(mid);112             ans += dinic();113             if(ans > 0) low = mid;114             else high = mid;115         }116         memset(vis,false,sizeof(vis));117         dfs2(S);118         vector<int>res;119         for(int i = 1; i < n; ++i)120         for(int j = head[i]; ~j; j = e[j].next)121             if(vis[i] != vis[e[j].to]) used[e[j].id] = true;122         for(int i = 1; i <= m; ++i)123             if(used[i]) res.push_back(i);124         printf("%d\n",res.size());125         for(int i = 0; i < res.size(); ++i)126             printf("%d%c",res[i],i == res.size()-1?\n: );127     }128     return 0;129 }
View Code

 

ZJU 2676 Network Wars