比赛 防止颓废的小练习v0.3 评测结果 AAAAAAAAA
题目名称 道路重建 最终得分 100
用户昵称 农场主 运行时间 0.028 s
代码语言 C++ 内存使用 1.00 MiB
提交时间 2016-10-19 17:17:25
显示代码纯文本
  1. #include<cstdio>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<queue>
  6. #define maxn 1000
  7. #define INF 1<<29
  8. using namespace std;
  9. class edge{
  10. public:
  11. int from,to,dis,w;
  12. };
  13. vector<edge> G[maxn];
  14. bool des[maxn][maxn]={0};
  15. int d[maxn]={0},inq[maxn]={0};
  16. int n,m,k,s,t;
  17. void spfa(int s,int t){
  18. for (int i=1;i<=n;i++){
  19. d[i]=INF;
  20. }
  21. d[s]=0; inq[s]=1;
  22. queue<int> q;
  23. q.push(s);
  24. while(!q.empty()){
  25. int x=q.front();q.pop(); inq[x]=0;
  26. for (int i=0;i<G[x].size();i++){
  27. edge& e=G[x][i];
  28. if (d[e.to]>d[e.from]+e.w){
  29. d[e.to]=d[e.from]+e.w;
  30. if (!inq[e.to]){
  31. inq[e.to]=1;
  32. q.push(e.to);
  33. }
  34. }
  35. }
  36. }
  37. printf("%d",d[t]);
  38. }
  39. void read(){
  40. scanf("%d%d",&n,&m);
  41. int u,v,w;
  42. for (int i=1;i<=m;i++){
  43. scanf("%d%d%d",&u,&v,&w);
  44. G[u].push_back((edge){u,v,w,0});
  45. G[v].push_back((edge){v,u,w,0});
  46. }
  47. scanf("%d",&k);
  48. for (int i=1;i<=k;i++){
  49. scanf("%d%d",&u,&v);
  50. des[u][v]=1;
  51. des[v][u]=1;
  52. }
  53. scanf("%d%d",&s,&t);
  54. for (int i=1;i<=n;i++){
  55. for (int j=0;j<G[i].size();j++){
  56. edge& e=G[i][j];
  57. if (des[e.from][e.to]==1){
  58. e.w=e.dis;
  59. }
  60. }
  61. }
  62. }
  63. int main(){
  64. freopen("rebuild.in","r",stdin);
  65. freopen("rebuild.out","w",stdout);
  66. read();
  67. spfa(s,t);
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.