acm专题---最短路
spfa的时间复杂度是0(e)
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
每组数据第一行包含两个正整数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),分别代表起点和终点。
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 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专题---最短路的更多相关文章
- acm专题---拓扑排序+优先队列
struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...
- acm专题---最小生成树
kruscal(eloge): 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N ...
- kuangbin专题最短路 D - Silver Cow Party
#include<iostream> #include<cstring> #include<algorithm> #include<iomanip> # ...
- PAT甲级专题|最短路
PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...
- acm专题---KMP模板
KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...
- acm专题--并查集
题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...
- acm专题---dfs+bfs
题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...
- acm专题---动态规划
题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096 #1400 : Composition 时间限制:10000ms 单点时限:1 ...
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
随机推荐
- 51nod 1292 字符串中的最大值V2(后缀自动机)
题意: 有一个字符串T.字符串S的F函数值可以如下计算:F(S) = L * S在T中出现的次数(L为字符串S的长度).求所有T的子串S中,函数F(S)的最大值. 题解: 求T的后缀自动机,然后所有每 ...
- byte数组转float实现与byte转换其它类型时进行&运算原理
下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] &a ...
- 【BZOJ4443】小凸玩矩阵(二分答案,二分图匹配)
[BZOJ4443]小凸玩矩阵(二分答案,二分图匹配) 题面 BZOJ Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两 ...
- BZOJ3782 上学路线 【dp + Lucas + CRT】
题目链接 BZOJ3782 题解 我们把终点也加入障碍点中,将点排序,令\(f[i]\)表示从\((0,0)\)出发,不经过其它障碍,直接到达\((x_i,y_i)\)的方案数 首先我们有个大致的方案 ...
- 《剑指offer》— JavaScript(5)用两个栈实现队列
用两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 实现代码 function Stack(){ var item = []; this. ...
- STL源码分析-hashtable
http://note.youdao.com/noteshare?id=5c8d2b09c0f72af9a12b0ed2023a338d
- CDN问题积累
我见到的CDN服务器只支持GET方法,只能以URL为索引来缓存内容. 有的时候我用相同的URL,相同的GET方法,但是不同Header参数时,后台对应的应该是不同的方法,返回不同的结果. 而这时候使用 ...
- [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes
引言 Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧. 例题 1 Given a 2D board cont ...
- CSS3方法总汇
PS:CSS3的3D和我做研发时的3D不一样他们只能旋转180度 横为X竖为Z高为Y transfrom:2D3D转换 rotareX:绕着X轴旋转 rotareY(-180deg):绕着Y轴旋转- ...
- react UI组件库 Salt UI
https://salt-ui.github.io/?spm=a219a.7629140.0.0.JWztQO