题目链接:http://poj.org/problem?id=1511

题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9。问你从起点到各个点以及从各个点到起点的最小路程总和。

解题思路:这里用了优先队列优化的dijkstra复杂度mlogn,从起点到个点最短路径直接算就好了,算各个点到起点的最短路径,只要把边的方向反一下,再算一次从起点到个点最短路径就好了。

Dijkstra:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<functional>
  5. #include<vector>
  6. #include<queue>
  7. using namespace std;
  8. typedef long long LL;
  9. typedef pair<int,int> p;
  10. const int N=1e6+;
  11. const int INF=<<;//这里不能比1e9小
  12. struct edge{
  13. int to,cost;
  14. };
  15.  
  16. vector<edge>eg[N];
  17. int V,E;
  18. bool used[N];
  19. int d[N];
  20. int a[N],b[N],c[N];
  21.  
  22. void dijkstra(int s){
  23. for(int i=;i<=V;i++){
  24. d[i]=INF;
  25. used[i]=false;
  26. }
  27. d[s]=;
  28.  
  29. priority_queue<p,vector<p>,greater<p> >q;
  30. q.push(p(,s));
  31. while(!q.empty()){
  32. p p1=q.top();
  33. q.pop();
  34. int v=p1.second;
  35. if(used[v]) continue;
  36. used[v]=true;
  37. for(int i=;i<eg[v].size();i++){
  38. edge e=eg[v][i];
  39. if(d[e.to]>d[v]+e.cost){
  40. d[e.to]=d[v]+e.cost;
  41. q.push(p(d[e.to],e.to));
  42. }
  43. }
  44. }
  45. }
  46.  
  47. int main(){
  48. int q;
  49. scanf("%d",&q);
  50. while(q--){
  51. scanf("%d %d",&V,&E);
  52. for(int i=;i<=V;i++){
  53. eg[i].clear();
  54. }
  55. for(int i=;i<=E;i++){
  56. scanf("%d%d%d",&a[i],&b[i],&c[i]);
  57. edge gg;
  58. gg.to=b[i],gg.cost=c[i];
  59. eg[a[i]].push_back(gg);
  60. }
  61. dijkstra();
  62. LL sum=;
  63. for(int i=;i<=V;i++){
  64. sum+=d[i];
  65. }
  66. //将边的方向反一下
  67. for(int i=;i<=V;i++){
  68. eg[i].clear();
  69. }
  70. for(int i=;i<=E;i++){
  71. edge gg;
  72. gg.to=a[i],gg.cost=c[i];
  73. eg[b[i]].push_back(gg);
  74. }
  75. dijkstra();
  76. for(int i=;i<=V;i++){
  77. sum+=d[i];
  78. }
  79. printf("%lld\n",sum);
  80. }
  81. }

SPFA:

  1. #include<iostream>
  2. #include<queue>
  3. #include<vector>
  4. using namespace std;
  5. const int INF=<<;
  6. const int N=1e6+;
  7.  
  8. struct edge{
  9. int to,cost;
  10. };
  11.  
  12. vector<edge>eg[N];
  13. int V,E;
  14. bool used[N];//是否在队列中
  15. int d[N];
  16. int a[N],b[N],c[N];
  17.  
  18. void spfa(int s){
  19. for(int i=;i<=V;i++){
  20. d[i]=INF;
  21. used[i]=false;
  22. }
  23. d[s]=;
  24.  
  25. queue<int>q;
  26. q.push(s);
  27. used[s]=true;
  28. while(!q.empty()){
  29. int v=q.front();
  30. q.pop();
  31. used[v]=false;
  32. for(int i=;i<eg[v].size();i++){
  33. edge e=eg[v][i];
  34. if(d[e.to]>d[v]+e.cost){
  35. d[e.to]=d[v]+e.cost;
  36. if(!used[e.to]){
  37. used[e.to]=true;
  38. q.push(e.to);
  39. }
  40. }
  41. }
  42. }
  43. }
  44.  
  45. int main(){
  46. int q;
  47. scanf("%d",&q);
  48. while(q--){
  49. scanf("%d%d",&V,&E);
  50. for(int i=;i<=V;i++){
  51. eg[i].clear();
  52. }
  53. for(int i=;i<=E;i++){
  54. scanf("%d%d%d",&a[i],&b[i],&c[i]);
  55. edge gg;
  56. gg.to=b[i],gg.cost=c[i];
  57. eg[a[i]].push_back(gg);
  58. }
  59. spfa();
  60. long long sum=;
  61. for(int i=;i<=V;i++){
  62. sum+=d[i];
  63. }
  64. //方向反一下
  65. for(int i=;i<=V;i++){
  66. eg[i].clear();
  67. }
  68. for(int i=;i<=E;i++){
  69. edge gg;
  70. gg.to=a[i],gg.cost=c[i];
  71. eg[b[i]].push_back(gg);
  72. }
  73. spfa();
  74. for(int i=;i<=V;i++){
  75. sum+=d[i];
  76. }
  77. printf("%lld\n",sum);
  78. }
  79. }

POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))的更多相关文章

  1. POJ 1511 - Invitation Cards (dijkstra优先队列)

    题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...

  2. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  3. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

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

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

  5. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

  6. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  7. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

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

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

  9. poj 1511 Invitation Cards spfa 邻接矩阵

    题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...

随机推荐

  1. Kerberos的白银票据详解

    0x01白银票据(Silver Tickets)定义 白银票据(Silver Tickets)是伪造Kerberos票证授予服务(TGS)的票也称为服务票据.如下图所示,与域控制器没有AS-REQ 和 ...

  2. python基础----__slots__方法、__call__方法

    ''' 1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的_ ...

  3. pycrypto 安装

    https://www.dlitz.net/software/pycrypto/ 下载pycrypto-2.6.1.tar.gz,解压后 python setup.py build python se ...

  4. MySQL自定义函数、触发器、存储过程

    存储过程 概念 存储过程,是一个数据库对象,类似一个函数. 在存储过程中可以使用SQL中的绝大部分内容,并且可以加入编程语言的特性(循环判断分支). 编写好存储过程之后,可以在客户端调用存储过程,存储 ...

  5. springMVC的controller返回值

    1.可以返回ModelAndView 2.可以返回一个String字符串:即一个jsp页面的逻辑视图名,这个在springMVC.xml中可以配置此页面逻辑视图的前缀和后缀 3.可以返回void类型: ...

  6. STL源码分析-bitset

    http://note.youdao.com/noteshare?id=0ea12c1fffd3866c6eddb8dc208c109d

  7. 【题解】Points, Lines and Ready-made Titles Codeforces 871C 图论

    Prelude 真是一道好题,然而比赛的时候花了太多时间在B题上,没时间想这个了QAQ. 题目链接:萌萌哒传送门(.^▽^) Solution 观察样例和样例解释,我们发现,假如有四个点,恰好占据在某 ...

  8. PL/SQL Developer 中的问题:Initialization error Could not load ".../oci.dll"解决方法

    ---------------------------------------------------------------------------------------------------- ...

  9. bootstrap模态框 内部input无法手动获取焦点

    //重写enforceFocus方法$(document).ready(function(){ $.fn.modal.Constructor.prototype.enforceFocus = func ...

  10. GridControl详解(三)列数据的格式设置

    为了测试方便,我们加入新的3列,格式分别是数据,时间,字符串.代码增加下列部分: //格式增加 dt.Columns.Add("数据",typeof(decimal)); dt.C ...