题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535

分析:

题意:求1点到其它点的最短距离之和+其它点到1点的最短距离之和

前面一部分直接用SPFA算法求出,而后一部分可用一数组存放反向边

(所有边的方向都反一下),利用反向边SPFA求出1点到其它点距离即可。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <queue>
  9.  
  10. using namespace std;
  11. const int inf = 0xfffffff;
  12. const int maxn = 1000000+10;
  13.  
  14. bool vis[maxn];
  15. int h1[maxn],h2[maxn],dis[maxn];
  16. int t1,t2,n,m;
  17.  
  18. struct node{
  19. int x,v,next;
  20. }f1[maxn],f2[maxn];
  21. ///f1存放顺向边, f2存放反向边
  22.  
  23. void init(){
  24. t1=t2=0;
  25. memset(h1,-1,sizeof(h1));
  26. memset(h2,-1,sizeof(h2));
  27. }
  28. void addnode_1(int a,int b,int c){
  29. f1[t1].x=b;
  30. f1[t1].v=c;
  31. f1[t1].next=h1[a];
  32. h1[a]=t1++;
  33. }
  34. void addnode_2(int a,int b,int c){
  35. f2[t2].x=b;
  36. f2[t2].v=c;
  37. f2[t2].next=h2[a];
  38. h2[a]=t2++;
  39. }
  40.  
  41. int spfa(node F[ ],int H[ ]){
  42. memset(vis,false,sizeof(vis));
  43. for(int i=1;i<=n;++i)
  44. dis[i]=inf;
  45. dis[1]=0;
  46. vis[1]=true;
  47. queue<int>M;
  48. M.push(1);
  49. while(!M.empty()){
  50. int now=M.front(); M.pop();
  51. vis[now]=false;
  52. for(int i=H[now];i!=-1;i=F[i].next){
  53. int next=F[i].x;
  54. if(dis[next]>dis[now]+F[i].v){
  55. dis[next]=dis[now]+F[i].v;
  56. if(!vis[next]){
  57. vis[next]=true;
  58. M.push(next);
  59. }
  60. }
  61. }
  62. }
  63. int sum=0;
  64. for(int i=2;i<=n;++i)
  65. sum+=dis[i];
  66. return sum;
  67. }
  68.  
  69. int main(){
  70. int T; scanf("%d",&T);
  71. while(T--){
  72. scanf("%d%d",&n,&m);
  73. init();
  74. while(m--){
  75. int a,b,c;
  76. scanf("%d%d%d",&a,&b,&c);
  77. addnode_1(a,b,c);
  78. addnode_2(b,a,c);
  79. }
  80. int ans=spfa(f1,h1)+spfa(f2,h2);
  81. cout<<ans<<endl;
  82. }
  83. return 0;
  84. }

HDU SPFA算法 Invitation Cards的更多相关文章

  1. (最短路 SPFA)Invitation Cards -- poj -- 1511

    链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...

  2. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  3. hdu 1535 Invitation Cards(spfa)

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  5. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  6. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  7. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

  8. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  9. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

随机推荐

  1. Windows 8.1 IIS 8.5 远程管理 Windows 2008 R2 IIS 7.0

    案例: Windows 8.1 x64 IIS 8.5 inetmgr_amd64_v1.1_en-US.msi Windows 2008 R2  x64 IIS  7.0 在Win8.1 通过IIS ...

  2. utf8_general_ci 、utf8_general_cs和utf8_bin的区别

    用了这么长时间,发现自己竟然不知道utf_bin和utf_general_ci这两者到底有什么区别..ci是 case insensitive, 即 "大小写不敏感", a 和 A ...

  3. struts2自己定义类型转换器

    1.1.  struts2自己定义类型转换器 1)        自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...

  4. CSS+DIV标签命名规范 搜索引擎最喜欢

    搜索引擎优化(seo)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面是目前流行的CSS+DIV的命名规则: 登录条:loginBar  标志:logo  侧栏:si ...

  5. authorization 元素(ASP.NET 设置架构)

    authorization 元素(ASP.NET 设置架构) 其他版本 1(共 1)对本文的评价是有帮助 - 评价此主题 [本文档仅供预览,在以后的发行版中可能会发生更改.包含的空白主题用作占位符.] ...

  6. javaweb学习路之四--cxf发布Webservice

    背景:maven构建的springMvc+mybatis框架 源码--->https://github.com/Zering/MyWeb 步骤:(本步骤是自己在实际探索过程中的步骤,我的思路是先 ...

  7. 实现AJAX局部刷新以及PageMethod方法的使用

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. HDU 1222(数论,最大公约数)

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  9. STL之vector(向量)

    C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的,这里详细介绍一下C++ Vector的相关应用技巧及基本内容: Construct vector #include ...

  10. 使用JS进行pc端、手机端判断

     <script type="text/javascript">            (function(){                var ua = nav ...