记录编号 144229 评测结果 AAAAAAAAAA
题目名称 [NOIP 2009]最优贸易 最终得分 100
用户昵称 Gravatarraywzy 是否通过 通过
代码语言 C++ 运行时间 0.308 s
提交时间 2014-12-21 15:20:50 内存使用 3.37 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<deque>
  5. #include<cstring>
  6. using namespace std;
  7. int F[100005];
  8. int MAXF[100005];
  9. int MINF[100005];
  10. vector<int> Adj1[100005];//正图
  11. vector<int> Adj2[100005];//反图
  12. int n,m,ANS=0;
  13. void Fbfs()//从n到1做bfs,中间注意Relax
  14. {
  15. int temp,i,P;deque<int> Q;
  16. Q.push_back(n);
  17. while(!Q.empty())
  18. {
  19. temp=Q.at(0);MAXF[temp]=max(MAXF[temp],F[temp]);Q.pop_front();
  20. for(i=0;i<Adj2[temp].size();i++)
  21. {
  22. P=Adj2[temp][i];
  23. if(MAXF[P]<MAXF[temp])
  24. {
  25. MAXF[P]=MAXF[temp];
  26. Q.push_back(P);
  27. }
  28. }
  29. }
  30. }
  31. void Sbfs()//从1到n做bfs,这次需要求到各点的最小值
  32. {
  33. int temp,i,P;deque<int> Q;
  34. Q.push_back(1);
  35. while(!Q.empty())
  36. {
  37. temp=Q.at(0);MINF[temp]=min(MINF[temp],F[temp]);Q.pop_front();
  38. for(i=0;i<Adj1[temp].size();i++)
  39. {
  40. P=Adj1[temp][i];
  41. if(MINF[temp]<MINF[P])
  42. {
  43. MINF[P]=MINF[temp];
  44. Q.push_back(P);
  45. }
  46. }
  47. }
  48. }
  49. int main()
  50. {
  51. freopen("trade.in","r",stdin);
  52. freopen("trade.out","w",stdout);
  53. int i,x,y,z,temp;
  54. memset(MAXF,0,sizeof(MAXF));
  55. memset(MINF,63,sizeof(MINF));
  56. scanf("%d%d",&n,&m);
  57. for(i=1;i<=n;i++)
  58. scanf("%d",&F[i]);
  59. for(i=1;i<=m;i++)
  60. {
  61. scanf("%d%d%d",&x,&y,&z);
  62. if(z==1) {Adj1[x].push_back(y);Adj2[y].push_back(x);}
  63. else {Adj1[x].push_back(y);Adj2[x].push_back(y);Adj1[y].push_back(x);Adj2[y].push_back(x);}
  64. }
  65. Fbfs();Sbfs();
  66. for(i=1;i<=n;i++)
  67. {
  68. temp=MAXF[i]-MINF[i];
  69. ANS=max(ANS,temp);
  70. }
  71. printf("%d\n",ANS);
  72. return 0;
  73. }