题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了)

有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和.

思路:

返回的时候是多个源,但是因为终点只有一个,所以把所有边反向之后, 再SPFA一次源即可.

  1. #include<cstdio>
  2. #include<vector>
  3. #include<queue>
  4. const int MAXN=1000000+10;
  5. typedef long long ll;
  6. const ll inf=1e60;
  7. using namespace std;
  8. struct Node{
  9. int v,w;
  10. };
  11. vector<Node>mp1[MAXN];//正向建图
  12. vector<Node>mp2[MAXN];//反向建图
  13. int n,m;
  14. ll cost[MAXN];
  15.  
  16. void SPFA(int u,vector<Node>mp[]){
  17. for(int i=2;i<=n;i++)cost[i]=inf;
  18. cost[1]=0;
  19. queue<int>Q;
  20. Q.push(u);
  21. while(!Q.empty()){
  22. int u=Q.front();
  23. Q.pop();
  24. for(int i=0;i<mp[u].size();i++){
  25. int v=mp[u][i].v;
  26. int w=mp[u][i].w;
  27. if(cost[v]>cost[u]+w){
  28. cost[v]=cost[u]+w;
  29. Q.push(v);
  30. }
  31. }
  32. }
  33. }
  34.  
  35. int main(){
  36. int _case;
  37. scanf("%d",&_case);
  38. while(_case--){
  39. scanf("%d%d",&n,&m);
  40. for(int i=1;i<=n;i++){
  41. mp1[i].clear();
  42. mp2[i].clear();
  43. }
  44. for(int i=1;i<=m;i++){
  45. int u,v,w;
  46. scanf("%d%d%d",&u,&v,&w);
  47. Node p1,p2;
  48. p1.v=u,p2.v=v,p1.w=p2.w=w;
  49. mp1[u].push_back(p2);
  50. mp2[v].push_back(p1);
  51. }
  52. SPFA(1,mp1);//正向求一次
  53. ll ans=0;
  54. for(int i=2;i<=n;i++){
  55. ans+=cost[i];
  56. }
  57. SPFA(1,mp2);//反向求一次
  58. for(int i=2;i<=n;i++){
  59. ans+=cost[i];
  60. }
  61. printf("%lld\n",ans);
  62. }
  63. return 0;
  64. }

自己敲一遍:

  1. #include <cstdio>
  2. #include <vector>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. typedef long long ll;
  7. const int MAXN = 1e6+5;
  8. const ll INF = 0x3f3f3f3f3f3f3f3fll;
  9. typedef struct node
  10. {
  11. int v,w;
  12. node(){}
  13. node(int _v, int _w):v(_v),w(_w){}
  14. }node;
  15.  
  16. int n,m;
  17. bool inq[MAXN];
  18. ll cost[MAXN];
  19. vector<node> g1[MAXN],g2[MAXN];
  20.  
  21. ll SPFA(int op)
  22. {
  23. memset(cost,0x3f,sizeof(cost));
  24. memset(inq,false,sizeof(inq));
  25. cost[1] = 0;
  26. queue<int> q;
  27. q.push(1);
  28. inq[1] = true;
  29. while(!q.empty())
  30. {
  31. int now = q.front();q.pop();
  32. inq[now] = false;
  33. for(int i=0,v,w;i<((op==1)?g1[now].size():g2[now].size());i++)
  34. {
  35. if(op==1)
  36. {
  37. v = g1[now][i].v, w = g1[now][i].w;
  38. }
  39. else
  40. {
  41. v = g2[now][i].v, w = g2[now][i].w;
  42. }
  43. if(cost[v] > cost[now] + w)
  44. {
  45. cost[v] = cost[now] + w;
  46. if(!inq[v])
  47. {
  48. q.push(v);
  49. inq[v] = true;
  50. }
  51. }
  52. }
  53. }
  54. ll ret = 0;
  55. for(int i=2;i<=n;i++)
  56. ret += cost[i];
  57. return ret;
  58. }
  59.  
  60. int main()
  61. {
  62. int T;
  63. scanf("%d",&T);
  64. while(T--)
  65. {
  66. scanf("%d %d",&n,&m);
  67. for(int i=1;i<=n;i++)
  68. {
  69. g1[i].clear();
  70. g2[i].clear();
  71. }
  72. for(int i=0,u,v,w;i<m;i++)
  73. {
  74. scanf("%d %d %d",&u,&v,&w);
  75. g1[u].push_back(node(v,w));
  76. g2[v].push_back(node(u,w));
  77. }
  78. ll ans = 0;
  79. ans += SPFA(1);
  80. ans += SPFA(2);
  81. printf("%d\n",(int)ans);
  82. }
  83. }

[HDU 1535]Invitation Cards[SPFA反向思维]的更多相关文章

  1. hdu 1535 Invitation Cards(SPFA)

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

  2. HDU 1535 Invitation Cards(SPFA,及其优化)

    题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...

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

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

  4. HDU - 1535 Invitation Cards 前向星SPFA

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

  5. hdu 1535 Invitation Cards(spfa)

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

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

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

  7. hdu 1535 Invitation Cards (最短路径)

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

  8. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  9. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

随机推荐

  1. rsync+sersync实现数据文件实时同步

    一.简介 sersync是基于Inotify开发的,类似于Inotify-tools的工具: sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字: ...

  2. C#在局域网中连接Liunx上的MySql数据库

    前期准备工作: 我所用的平台是VS2010和Ubuntu 14.04.3  LTS 一.由于MySql并没有集成在VS2010中所以要先安装MySQL Connector Net 6.9.8连接工具, ...

  3. Javscript中的null和undefined

    1.null是JavaScript关键字,含义是“非对象”,它可以表示数字.字符串和对象是“无值”的. var x = null; typeof x ;//返回“object” var x=null, ...

  4. oracle 的服务器进程(PMON, SMON,CKPT,DBWn,LGWR,ARCn)

    来着TOM的<oracle 编程艺术 9i,10g,11g> PMON PMON,进程监视.PMON主要有3个用途: 1,在进程非正常中断后,做清理工作.例如:dedicated serv ...

  5. PHP MAIL DEMO(程序代码直接发送邮件)

    php代码 <?php // 收件人邮箱地址 $to = 'xxxxxx@qq.com'; // 邮件主题 $title = '测试邮件发送'; // 邮件内容 $msg = '这是一封测试邮件 ...

  6. VC中窗口ID,句柄,指针三者相互转换函数【转】

    ID--HANDLE--HWND三者之间的互相转换id->句柄        hWnd = ::GetDlgItem(hParentWnd,id);id->指针        CWnd:: ...

  7. DOM 节点实例操作

    涉及知识点包括节点的所有知识 目的: 自动为文档创建一个目录表 自动创建目录

  8. 【转】10 个迅速提升你 Git 水平的提示

    最近我们推出了两个教程:熟悉Git的基本功能和让你在开发团队中熟练的使用Git . 我们所讨论的命令足够一个开发者在Git使用方面游刃有余.在这篇文章中,我们试图探索怎样有效的管理你的时间和充分的使用 ...

  9. HBase -ROOT-和.META.表结构(region定位原理)

    在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...

  10. 双人贪吃蛇小游戏C++原创

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang /*贪吃蛇*/ #include<stdio.h> #include<time. ...