Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1.  
  2. #include<cstdio>
  3. #include<vector>
  4. #include<bitset>
  5.  
  6. using namespace std;
  7.  
  8. int i,j,n,m,k,nr,tata[256],total;
  9. short int cost[256][256],h[256],cat[256],ajung[256],st[456],dr[456],f[256],unde[256];
  10.  
  11. vector<int> a[256];
  12. bitset<256> fol;
  13.  
  14. void upheap(int poz)
  15. {
  16. int tata=poz/2;
  17.  
  18. if(tata>0&&cat[h[tata]]>cat[h[poz]])
  19. {
  20. int aux=unde[h[tata]];
  21. unde[h[tata]]=unde[h[poz]];
  22. unde[h[poz]]=aux;
  23.  
  24. aux=h[tata];
  25. h[tata]=h[poz];
  26. h[poz]=aux;
  27.  
  28. poz=tata;
  29. tata=poz/2;
  30. }
  31. }
  32.  
  33. int min(int x,int y)
  34. {
  35. if(cat[h[x]]<cat[h[y]]) return x;
  36. return y;
  37. }
  38.  
  39. void downheap(int poz)
  40. {
  41. int s=poz/2,d=poz/2+1;
  42.  
  43. while(cat[h[s]]<cat[h[poz]]||cat[h[d]]<cat[h[poz]])
  44. {
  45. int fiu=min(s,d);
  46.  
  47. int aux=unde[h[fiu]];
  48. unde[h[fiu]]=unde[h[poz]];
  49. unde[h[poz]]=aux;
  50.  
  51. aux=h[fiu];
  52. h[fiu]=h[poz];
  53. h[poz]=aux;
  54.  
  55. poz=fiu;
  56. s=poz/2;
  57. d=poz/2+1;
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. freopen("online.in","r",stdin);
  64. freopen("online.out","w",stdout);
  65.  
  66. scanf("%d%d",&n,&m);
  67.  
  68. for(i=1;i<=m;++i)
  69. {
  70. int x,y,co;
  71. scanf("%d%d%d",&x,&y,&co);
  72.  
  73. cost[x][y]=cost[y][x]=co;
  74. a[x].push_back(y);
  75. a[y].push_back(x);
  76.  
  77. }
  78.  
  79. h[1]=1;
  80. unde[1]=1;
  81. ++nr;
  82. cat[0]=30000;
  83.  
  84. while(nr)
  85. {
  86. int u=h[1];
  87. total+=cat[u];
  88. tata[u]=ajung[u];
  89. fol[u]=1;
  90. unde[u]=0;
  91. h[1]=h[nr];
  92. unde[h[nr]]=1;
  93. h[nr]=0;
  94. --nr;
  95. downheap(1);
  96.  
  97. for(i=0;i<a[u].size();++i)
  98. if(!fol[a[u][i]])
  99. {
  100. int fiu=a[u][i];
  101.  
  102. if(unde[fiu])
  103. {
  104. if(cost[u][fiu]<cat[fiu])
  105. {
  106. cat[fiu]=cost[u][fiu];
  107. ajung[fiu]=u;
  108. upheap(unde[fiu]);
  109. }
  110. }
  111. else
  112. {
  113. h[++nr]=fiu;
  114. unde[fiu]=nr;
  115. cat[fiu]=cost[u][fiu];
  116. ajung[fiu]=u;
  117. upheap(nr);
  118. }
  119. }
  120. }
  121.  
  122. printf("%d\n",total);
  123.  
  124. scanf("%d",&k);
  125.  
  126. for(i=1;i<=k;++i)
  127. {
  128. st[0]=0;
  129. dr[0]=0;
  130. int x,y,co;
  131. scanf("%d%d%d",&x,&y,&co);
  132.  
  133. if((x==tata[y]||y==tata[x])&&co<cost[x][y])
  134. {
  135. total-=cost[x][y];
  136. total+=co;
  137. cost[x][y]=cost[y][x]=co;
  138. printf("%d\n",total);
  139. continue;
  140. }
  141.  
  142. int s=x,d=y,p=0,q=0,cmax=0,ok=1,com=0;
  143.  
  144. while(s>0)
  145. {
  146. f[s]=i;
  147. s=tata[s];
  148. }
  149.  
  150. while(d>0)
  151. {
  152. int tat=tata[d];
  153. dr[++dr[0]]=d;
  154. if(f[d]==i)
  155. {
  156. com=d;
  157. break;
  158. }
  159. if(cost[d][tat]>cmax)
  160. {
  161. cmax=cost[d][tat];
  162. p=d;
  163. q=tat;
  164. ok=0;
  165. }
  166. d=tat;
  167. }
  168.  
  169. s=x;
  170. while(s!=com)
  171. {
  172. int tat=tata[s];
  173. st[++st[0]]=s;
  174. if(cost[s][tat]>cmax)
  175. {
  176. cmax=cost[s][tat];
  177. p=s;
  178. q=tat;
  179. ok=1;
  180. }
  181. s=tat;
  182. }
  183.  
  184. if(co<cmax)
  185. {
  186. if(ok)
  187. {
  188. j=st[0];
  189. while(st[j]!=p) --j;
  190. for(;j>1;--j) tata[st[j]]=st[j-1];
  191. tata[st[1]]=y;
  192. total-=cmax;
  193. total+=co;
  194. cost[x][y]=cost[y][x]=co;
  195. printf("%d\n",total);
  196. }
  197. else
  198. {
  199. j=dr[0];
  200. while(dr[j]!=p) --j;
  201. for(;j>1;--j) tata[dr[j]]=dr[j-1];
  202. tata[dr[1]]=x;
  203. total-=cmax;
  204. total+=co;
  205. cost[x][y]=cost[y][x]=co;
  206. printf("%d\n",total);
  207. }
  208. }
  209. else printf("%d\n",total);
  210. }
  211.  
  212. fclose(stdin);
  213. fclose(stdout);
  214.  
  215. return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement