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)

#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;
}
};
struct cmp{
bool operator()(edge a,edge b)
{
return a.val>b.val;
}
};
vector<vector<edge>> edges;
bool vis[maxm];
int dis[maxm];
int mymap[maxm][maxm];
int n,m; void dijkstrapriority(int s,int e)
{
for(int i=0;i<n;i++)
{
vis[i]=false;
dis[i]=INF;
}
priority_queue<edge,vector<edge>,cmp> myque;
vis[s]=true;
dis[s]=0;
for(int i=0;i<edges[s].size();i++)
{
myque.push(edge(edges[s][i].to,edges[s][i].val));
dis[edges[s][i].to]=edges[s][i].val;
}
while (!myque.empty()) {
edge toptmp=myque.top();
myque.pop();
if(vis[toptmp.to]) continue;
vis[toptmp.to]=true;
for(int i=0;i<edges[toptmp.to].size();i++)
{
int t=edges[toptmp.to][i].to;
if(!vis[t]&&dis[t]>dis[toptmp.to]+edges[toptmp.to][i].val)
{
dis[t]=dis[toptmp.to]+edges[toptmp.to][i].val;
myque.push(edge(t,dis[t]));
}
}
}
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);
dijkstrapriority(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 */

  

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. UVA - 11997(巧妙的优先队列)

    题意: 有k个整数数组,各包含k个元素,在每个数组中取一个元素加起来,可以得到kk个和,求这些和中最小的k个值 解析: 从简单的情况开始分析:经典方法,对原题没有思路,那么分析问题的简化版 这是对于两 ...

  2. 【转】NHibernate 各种数据库配置

    转载自:http://terrylee.cnblogs.com/archive/2006/04/05/367381.html 自己只用了Oracle 可用! 三.常见的配置示例 Castle网站为我们 ...

  3. 【ListBox】ListBox的相关操作

    Winform中两个listbox的操作是平时比较常用的操作. 本次将以一个Winform实例来分享一下两个listbox的操作,包括:listbox添加项,项的上移下移等操作. 假设有两个listb ...

  4. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  5. 洛谷 P4585 [FJOI2015]火星商店问题 解题报告

    P4585 [FJOI2015]火星商店问题 题目描述 火星上的一条商业街里按照商店的编号\(1,2,\dots,n\) ,依次排列着\(n\)个商店.商店里出售的琳琅满目的商品中,每种商品都用一个非 ...

  6. 第三周——构建一个简单的Linux系统MenuOS

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个 ...

  7. Hbase(四) 过滤器查询

    引言:过滤器的类型很多,但是可以分为两大类——比较过滤器,专用过滤器过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端: 一.hbase过滤器的分类 1.比较过滤器 行键过 ...

  8. 用Visual C#开发基于OpenCV的Windows应用程序

    http://blog.163.com/wangxh_jy/blog/static/28233883201001581640283/ 关于详细的配置及程序运行截图,请下载:http://downloa ...

  9. Codeforces 932.E Team Work

    E. Team Work time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  10. 题解【bzoj3240 [NOI2013]矩阵游戏】

    挖坑2333 等我把代码写完了再写