1 #include<cstdio> 2 #include<iostream> 3 #define M 10000 4 #define inf 0x7fffffff 5 #include<cstring> 6 #define eps 1e-5 7 using namespace std; 8 struct data 9 { 10 int x,y; 11 double z; 12 }a[M]; 13 int d[M],q[M],S,T,cnt=1,n,m,head[M],next[M],u[M]; 14 double p,w[M],l,r,ss,ans,ans1; 15 void jia1(int a1,int a2,double a3) 16 { 17 cnt++; 18 next[cnt]=head[a1]; 19 head[a1]=cnt; 20 u[cnt]=a2; 21 w[cnt]=a3; 22 return; 23 } 24 void jia(int a1,int a2,double a3) 25 { 26 jia1(a1,a2,a3); 27 jia1(a2,a1,0); 28 return; 29 } 30 bool bfs() 31 { 32 memset(d,0,sizeof(int)*(T+1)); 33 int h=0,t=1; 34 q[1]=S; 35 d[S]=1; 36 for(;h<t;) 37 { 38 h++; 39 int p=q[h]; 40 for(int i=head[p];i;i=next[i]) 41 if(!d[u[i]]&&w[i]) 42 { 43 d[u[i]]=d[p]+1; 44 if(d[T]) 45 return 1; 46 t++; 47 q[t]=u[i]; 48 } 49 } 50 return 0; 51 } 52 double dinic(int s,double f) 53 { 54 if(s==T) 55 return f; 56 double rest=f; 57 for(int i=head[s];i&&rest;i=next[i]) 58 if(w[i]&&d[u[i]]==d[s]+1) 59 { 60 double now=dinic(u[i],min(rest,w[i])); 61 if(!now) 62 d[u[i]]=0; 63 w[i]-=now; 64 w[i^1]+=now; 65 rest-=now; 66 } 67 return f-rest; 68 } 69 void jian(double mi) 70 { 71 cnt=1; 72 memset(head,0,sizeof(int)*(T+1)); 73 for(int i=1;i<=m;i++) 74 if(a[i].z<mi) 75 jia(a[i].x,a[i].y,a[i].z); 76 else 77 jia(a[i].x,a[i].y,mi); 78 return; 79 } 80 int main() 81 { 82 scanf("%d%d%lf",&n,&m,&p); 83 for(int i=1;i<=m;i++) 84 { 85 scanf("%d%d%lf",&a[i].x,&a[i].y,&a[i].z); 86 jia(a[i].x,a[i].y,a[i].z); 87 } 88 S=1; 89 T=n; 90 for(;bfs();) 91 ans+=dinic(S,inf); 92 printf("%d ",(int)ans); 93 l=0; 94 r=5000000; 95 for(;r-l>eps;) 96 { 97 double mid=(l+r)/2; 98 jian(mid); 99 ans1=0; 100 for(;bfs();) 101 ans1+=dinic(S,inf); 102 if(ans1==ans) 103 { 104 ss=mid; 105 r=mid; 106 } 107 else 108 l=mid; 109 } 110 printf("%.4lf",ss*p); 111 return 0; 112 }
贪心 BOB肯定全加在最大权值的边上,二分权值网络流。