spfa的时间复杂度是0(e)

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874

Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

 
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
 
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
 
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
 
Sample Output
2
-1

#include <iostream>

using namespace std;

#include <vector>

#include<algorithm>

#include<queue>

#include<string>

#include<map>

#include<math.h>

#include<iomanip>

#include<stack>

#include<string.h>

const int maxm=201;

const int INF=0X7FFFFFFF;

struct edge {

int to;

int val;

edge(int _to,int _val)

{

to=_to;

val=_val;

}

};

vector<vector<edge>> edges;

bool vis[maxm];

int dis[maxm];

int mymap[maxm][maxm];

int n,m;

void spfa(int s,int e)

{

queue<int> que;

que.push(s);

vis[s]=true;

dis[s]=0;

while(!que.empty())

{

int toptmp=que.front();

que.pop();

for(int i=0;i<edges[toptmp].size();i++)

{

if(dis[edges[toptmp][i].to]>dis[toptmp]+edges[toptmp][i].val)

{

dis[edges[toptmp][i].to]=dis[toptmp]+edges[toptmp][i].val;

if(!vis[edges[toptmp][i].to])

{

vis[edges[toptmp][i].to]=true;

que.push(edges[toptmp][i].to);

}

}

}

vis[toptmp]=false;

}

if(dis[e]==INF)

cout<<"-1"<<endl;

else

cout<<dis[e]<<endl;

}

int main()

{

while (cin>>n>>m) {

edges.clear();

edges.resize(n+1);

for(int i=0;i<n;i++)

{

dis[i]=INF;

vis[i]=false;

}

for(int i=0;i<m;i++)

{

int x,y,z;

cin>>x>>y>>z;

bool flag1=true,flag2=true;

for(int j=0;j<edges[x].size();j++)

{

if(edges[x][j].to==y)

{

if(edges[x][j].val>z)

edges[x][j].val=z;

flag1=false;

}

}

if(flag1)

edges[x].push_back(edge(y,z));

for(int j=0;j<edges[y].size();j++)

{

if(edges[y][j].to==x)

{

if(edges[y][j].val>z)

edges[y][j].val=z;

flag2=false;

}

}

if(flag2)

edges[y].push_back(edge(x,z));

}

int s,e;

cin>>s>>e;

spfa(s,e);

}

return 0;

}

/*

3 4

0 1 1

0 2 3

0 2 2

1 2 1

0 2

3 1

0 1 1

1 1

3 4

1 0 3

0 1 1

0 2 3

1 2 1

0 2

2

-1

*/

 

dijikstra +优先队列 o(vlogv)

  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<string>
  7. #include<map>
  8. #include<math.h>
  9. #include<iomanip>
  10. #include<stack>
  11. #include<string.h>
  12. const int maxm=201;
  13. const int INF=0X7FFFFFFF;
  14. struct edge {
  15. int to;
  16. int val;
  17. edge(int _to,int _val)
  18. {
  19. to=_to;
  20. val=_val;
  21. }
  22. };
  23. struct cmp{
  24. bool operator()(edge a,edge b)
  25. {
  26. return a.val>b.val;
  27. }
  28. };
  29. vector<vector<edge>> edges;
  30. bool vis[maxm];
  31. int dis[maxm];
  32. int mymap[maxm][maxm];
  33. int n,m;
  34.  
  35. void dijkstrapriority(int s,int e)
  36. {
  37. for(int i=0;i<n;i++)
  38. {
  39. vis[i]=false;
  40. dis[i]=INF;
  41. }
  42. priority_queue<edge,vector<edge>,cmp> myque;
  43. vis[s]=true;
  44. dis[s]=0;
  45. for(int i=0;i<edges[s].size();i++)
  46. {
  47. myque.push(edge(edges[s][i].to,edges[s][i].val));
  48. dis[edges[s][i].to]=edges[s][i].val;
  49. }
  50. while (!myque.empty()) {
  51. edge toptmp=myque.top();
  52. myque.pop();
  53. if(vis[toptmp.to]) continue;
  54. vis[toptmp.to]=true;
  55. for(int i=0;i<edges[toptmp.to].size();i++)
  56. {
  57. int t=edges[toptmp.to][i].to;
  58. if(!vis[t]&&dis[t]>dis[toptmp.to]+edges[toptmp.to][i].val)
  59. {
  60. dis[t]=dis[toptmp.to]+edges[toptmp.to][i].val;
  61. myque.push(edge(t,dis[t]));
  62. }
  63. }
  64. }
  65. if(dis[e]==INF)
  66. cout<<"-1"<<endl;
  67. else
  68. cout<<dis[e]<<endl;
  69. }
  70.  
  71. int main()
  72. {
  73.  
  74. while (cin>>n>>m) {
  75. edges.clear();
  76. edges.resize(n+1);
  77.  
  78. for(int i=0;i<n;i++)
  79. {
  80. dis[i]=INF;
  81. vis[i]=false;
  82. }
  83. for(int i=0;i<m;i++)
  84. {
  85. int x,y,z;
  86. cin>>x>>y>>z;
  87. bool flag1=true,flag2=true;
  88. for(int j=0;j<edges[x].size();j++)
  89. {
  90. if(edges[x][j].to==y)
  91. {
  92. if(edges[x][j].val>z)
  93. edges[x][j].val=z;
  94. flag1=false;
  95. }
  96. }
  97. if(flag1)
  98. edges[x].push_back(edge(y,z));
  99. for(int j=0;j<edges[y].size();j++)
  100. {
  101. if(edges[y][j].to==x)
  102. {
  103. if(edges[y][j].val>z)
  104. edges[y][j].val=z;
  105. flag2=false;
  106. }
  107. }
  108. if(flag2)
  109. edges[y].push_back(edge(x,z));
  110. }
  111.  
  112. int s,e;
  113. cin>>s>>e;
  114. //spfa(s,e);
  115. dijkstrapriority(s,e);
  116. }
  117. return 0;
  118. }
  119. /*
  120.  
  121. 3 4
  122. 0 1 1
  123. 0 2 3
  124. 0 2 2
  125. 1 2 1
  126. 0 2
  127.  
  128. 3 1
  129. 0 1 1
  130. 1 1
  131.  
  132. 3 4
  133. 1 0 3
  134. 0 1 1
  135. 0 2 3
  136. 1 2 1
  137. 0 2
  138.  
  139. 2
  140. -1
  141.  
  142. */

  

acm专题---最短路的更多相关文章

  1. acm专题---拓扑排序+优先队列

    struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...

  2. acm专题---最小生成树

    kruscal(eloge): 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N ...

  3. kuangbin专题最短路 D - Silver Cow Party

    #include<iostream> #include<cstring> #include<algorithm> #include<iomanip> # ...

  4. PAT甲级专题|最短路

    PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...

  5. acm专题---KMP模板

    KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...

  6. acm专题--并查集

    题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...

  7. acm专题---dfs+bfs

    题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...

  8. acm专题---动态规划

    题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096 #1400 : Composition 时间限制:10000ms 单点时限:1 ...

  9. acm专题---键树

    题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...

随机推荐

  1. 转载--------Python中:self和__init__的含义 + 为何要有self和__init__

    背景 回复:我写的一些Python教程,需要的可以看看,中SongShouJiong的提问: Python中的self,__init__的含义是啥?为何要有self,__init这些东西? 解释之前, ...

  2. linux 递归删除目录文件

    比如删.svn文件 >find . -name ".svn" | xargs -exec rm -rf

  3. Myeclipse下配置SVN报错问题 svn: E175002: java.lang.RuntimeException: Could not generate DH keypair,缺少subclipse插件的javaHL

    在myeclipse10.0下安装svn插件,出现了Could not generate DH keypair,这么一个错误. 看到了一篇博客说是svn接口选择问题,可是我myeclipse没有那个接 ...

  4. Codeforces Round #404 (Div. 2)A B C二分

    A. Anton and Polyhedrons time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. poj1486 Sorting Slides

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4812   Accepted: 1882 De ...

  6. POJ2975:Nim(Nim博弈)

    Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7279   Accepted: 3455 题目链接:http://p ...

  7. portal商品展示功能逻辑

    看下接口: 返回值: 门户商品搜索功能的实现: 根据分类id进行搜索,根据关键词进行搜索,并按照一定的顺序排序 业务逻辑: 1.查询分类是否存在. 2.如果分类存在,则递归分类,展示父类商品,子类商品 ...

  8. C#中调用Dll动态链接库

    C#中调用Dll动态链接库 起始 受限于语言的不同,我们有的时候可能会用别人提供的函数及方法 或者其他的什么原因.反正就是要调!!! 恰巧别人所使用的的语言跟自己又不是一样的 这个时候想要调用别人的函 ...

  9. 【Java】将字符串转化为整数

    前几天面试遇到这个问题:在Java中如何将字符串转化为整数,当时too young too naive,随便回答了一下.今天跑去看Java源码中paresInt函数的写法,Oh my god!其实不看 ...

  10. TabLayout 使用方法 (基础)

    此为布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a ...