记录编号 300581 评测结果 AAAAAAAAAAAA
题目名称 [USACO Feb05] 神秘的挤奶机 最终得分 100
用户昵称 Gravatarmildark 是否通过 通过
代码语言 C++ 运行时间 1.138 s
提交时间 2016-08-28 18:50:43 内存使用 0.32 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<vector>
  6. #include<cassert>
  7. using namespace std;
  8. const int INF=0x7fffffff/2;
  9. const int SIZEN=210;
  10. class Edge{
  11. public:
  12. int from,to,cap,flow,cost;
  13. };
  14. vector<Edge> edges;
  15. vector<int> c[SIZEN];
  16. void addedge(int from,int to,int cap,int cost){
  17. edges.push_back((Edge){from,to,cap,0,cost});
  18. edges.push_back((Edge){to,from,cap,0,cost});
  19. int tot=edges.size()-2;
  20. c[from].push_back(tot);
  21. c[to].push_back(tot+1);
  22. }
  23. int N,S,T;
  24. int f[SIZEN],father[SIZEN];
  25. int ansflow=0,anscost=0;
  26. bool Prim_one(int S){
  27. static bool used[SIZEN];
  28. for(int i=0;i<=N;i++) f[i]=INF;
  29. memset(used,0,sizeof(used));
  30. f[S]=0;
  31. while(true){
  32. int x=-1;
  33. for(int i=0;i<=N;i++){
  34. if(used[i]) continue;
  35. if(x==-1||f[i]<f[x]) x=i;
  36. }
  37. if(x==-1) break;
  38. used[x]=true;
  39. for(int i=0;i<c[x].size();i++){
  40. Edge &e=edges[c[x][i]];
  41. if(e.cap<=e.flow) continue;
  42. if(used[e.to]) continue;
  43. int now;
  44. if(e.flow==0) now=max(f[x],e.cost);
  45. else if(e.flow<0) now=f[x];
  46. if(now<f[e.to]){
  47. f[e.to]=now;
  48. father[e.to]=c[x][i];
  49. }
  50. }
  51. }
  52. if(!used[T]) return false;
  53. ansflow++;
  54. anscost=max(anscost,f[T]);
  55. int x=T;
  56. while(x!=S){
  57. int now=father[x];
  58. edges[now].flow++;
  59. edges[now^1].flow--;
  60. x=edges[now].from;
  61. }
  62. return true;
  63. }
  64. int MCMF(int mustflow){
  65. ansflow=anscost=0;
  66. for(int i=1;i<=mustflow;i++){
  67. assert(Prim_one(S));
  68. }
  69. return anscost;
  70. }
  71. int n,m,rides;
  72. void makegraph(void){
  73. scanf("%d%d%d",&n,&m,&rides);
  74. N=n;S=1;T=N;
  75. int a,b,w;
  76. for(int i=1;i<=m;i++){
  77. scanf("%d%d%d",&a,&b,&w);
  78. addedge(a,b,1,w);
  79. }
  80. }
  81. int main(){
  82. freopen("secretmilking.in","r",stdin);
  83. freopen("secretmilking.out","w",stdout);
  84. makegraph();
  85. printf("%d\n",MCMF(rides));
  86. return 0;
  87. }
  88.