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
题意:
题解:
#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 ",(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()? ' ':' '); } } return 0; }