Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
深搜写的TLE与16MS16ms: #include <iostream> using namespace std; int ma[250][250]; int pre[250]; int q[250]; int n; bool dfs(int v) { if (v==n) { return 1; } int i; for (i=1;i<=n;i++) { if (ma[v][i]>0&&pre[i]==-1) { pre[i]=v; q[i]=q[v]>ma[v][i]?ma[v][i]:q[v]; if (dfs(i)) return 1; } } return 0; } int maxflow() { int maxf=0; while (1) { memset(pre,0,sizeof(pre)); int i; for (i=1;i<=n;i++) { q[i]=INT_MAX; pre[i]=EOF; } pre[1]=-2; if (dfs(1)) { int z=n; while (pre[z]!=-2) { int zz=pre[z]; ma[zz][z]-=q[n]; ma[z][zz]+=q[n]; z=zz; } maxf+=q[n]; } else { break; } } return maxf; } int main() { int k; while (cin>>k>>n) { memset(ma,0,sizeof(ma)); while (k--) { int a,b,c; cin>>a>>b>>c; ma[a][b]+=c; } cout<<maxflow()<<endl; } return 0; } TLE: #include <iostream> using namespace std; int ma[250][250]; int pre[250]; int q[250]; int n; bool dfs(int v) { if (v==n) { return 1; } int i; for (i=1;i<=n;i++) { if (ma[v][i]&&pre[i]==-1) { pre[i]=v; q[i]=q[v]>ma[v][i]?ma[v][i]:q[v]; if (dfs(i)) return 1; } } return 0; } int maxflow() { int maxf=0; while (1) { memset(pre,0,sizeof(pre)); int i; for (i=1;i<=n;i++) { q[i]=INT_MAX; pre[i]=EOF; } pre[1]=-2; if (dfs(1)) { int z=n; while (pre[z]!=-2) { int zz=pre[z]; ma[zz][z]-=q[n]; ma[z][zz]+=q[n]; z=zz; } maxf+=q[n]; } else { break; } } return maxf; } int main() { int k; while (cin>>k>>n) { memset(ma,0,sizeof(ma)); while (k--) { int a,b,c; cin>>a>>b>>c; ma[a][b]+=c; } cout<<maxflow()<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator