记录编号 204650 评测结果 AAAAAAAAAA
题目名称 [SYOI 2015] Asm.Def的打击序列 最终得分 100
用户昵称 Gravatardydxh 是否通过 通过
代码语言 C++ 运行时间 0.395 s
提交时间 2015-11-04 15:32:53 内存使用 1.27 MiB
显示代码纯文本
  1. /*
  2. Problem:cogs2084;
  3. Language:c++;
  4. by dydxh;
  5. 2015.11.03;
  6. */
  7. #include<iostream>
  8. #include<cstdio>
  9. #include<set>
  10. #include<map>
  11. #include<queue>
  12. #include<algorithm>
  13. #include<cstring>
  14. #include<cmath>
  15. #include<utility>
  16. #include<ctime>
  17. #include<cstdlib>
  18. #include<bitset>
  19. #include<string>
  20. #define ll long long
  21. #define ull unsigned long long
  22. using namespace std;
  23. const int oo=2000000000;
  24. const int maxn=505;
  25. const int maxm=30005;
  26. int n,m,Coster,super_s,super_e,cnt=1,ans;
  27. int linkk[maxn<<1];
  28. struct edge{
  29. int y,next,c,v;
  30. }e[(maxn*3)+(maxm<<1)];
  31. inline int read(){
  32. int x=0;bool flag=0;char ch=getchar();
  33. while(ch>'9' || ch<'0') {if(ch=='-') flag=1;ch=getchar();}
  34. while(ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
  35. return flag?-x:x;
  36. }
  37. void insert(int a,int b,int c,int d){
  38. e[++cnt].next=linkk[a];
  39. linkk[a]=cnt;
  40. e[cnt].y=b;
  41. e[cnt].v=c;
  42. e[cnt].c=d;
  43. e[++cnt].next=linkk[b];
  44. linkk[b]=cnt;
  45. e[cnt].y=a;
  46. e[cnt].v=0;
  47. e[cnt].c=-d;
  48. }
  49. void Build_Graph(){
  50. n=read(),m=read(),Coster=read();
  51. super_s=0,super_e=n<<1|1;
  52. for(int i=1;i<=m;i++){
  53. int x=read(),y=read(),v=read();
  54. if(v>=Coster) continue;
  55. insert(x,y+n,1,v);
  56. }
  57. for(int i=1;i<=n;i++){
  58. insert(super_s,i,1,0);
  59. insert(super_s,i+n,1,Coster);
  60. insert(i+n,super_e,1,0);
  61. }
  62. }
  63. int head,tail;
  64. int q[maxn+20],pos[maxn],pre[maxn],Dis[maxn];
  65. bool vis[maxn];
  66. bool SPFA(){
  67. memset(Dis,10,sizeof(Dis));
  68. Dis[super_s]=0,q[1]=super_s,head=0,tail=1;
  69. while(head!=tail){
  70. head++;if(head>maxn) head=1;
  71. int tn=q[head];
  72. for(int i=linkk[tn];i;i=e[i].next){
  73. if(e[i].v==0) continue;
  74. int tmp=e[i].y;
  75. if(Dis[tmp]>Dis[tn]+e[i].c){
  76. Dis[tmp]=Dis[tn]+e[i].c;
  77. pre[tmp]=tn,pos[tmp]=i;
  78. if(!vis[tmp]){
  79. vis[tmp]=true;
  80. tail++;if(tail>maxn) tail=1;
  81. q[tail]=tmp;
  82. }
  83. }
  84. }
  85. vis[tn]=false;
  86. }
  87. return Dis[super_e]!=Dis[super_e+1];
  88. }
  89. void dydxh(){
  90. int flow=oo;
  91. for(int i=super_e;i!=super_s;i=pre[i])
  92. if(e[pos[i]].v<flow)
  93. flow=e[pos[i]].v;
  94. ans+=flow*Dis[super_e];
  95. for(int i=super_e;i!=super_s;i=pre[i]){
  96. e[pos[i]].v-=flow;
  97. e[pos[i]^1].v+=flow;
  98. }
  99. }
  100. int main()
  101. {
  102. //freopen("cc.in","r",stdin);
  103. freopen("asm_lis.in","r",stdin);
  104. freopen("asm_lis.out","w",stdout);
  105. Build_Graph();
  106. while(SPFA())
  107. dydxh();
  108. printf("%d\n",ans);
  109. return 0;
  110. }