acm专题---最短路
2017-02-10 17:23
295 查看
spfa的时间复杂度是0(e)
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874
[align=left]Problem Description[/align]
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
[align=left]Input[/align]
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数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),分别代表起点和终点。
[align=left]Output[/align]
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
[align=left]Sample Input[/align]
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
[align=left]Sample Output[/align]
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)
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874
[align=left]Problem Description[/align]
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
[align=left]Input[/align]
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数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),分别代表起点和终点。
[align=left]Output[/align]
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
[align=left]Sample Input[/align]
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
[align=left]Sample Output[/align]
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暑期集训——专题二[最短路Floyd算法]
- ACM暑期集训——专题一[最短路Dijkstra算法]
- ACM 暑期培训——最短路/最小生成树专题
- ACM暑期集训——专题二[最短路Bellman-Ford算法]
- ACM: 条件最短路 poj 1724 (没剪枝…
- [kuangbin带你飞]专题四 最短路练习 B
- ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544
- ACM暑假集训第二章——最短路
- [专题-图论]最短路
- ACM模板 图论,Floyd 任意两点间最短路
- 最短路专题
- ACM暑期集训——专题一[DFS]
- ACM_bellman ford(单源最短路)
- ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544
- kuangbin带我飞 专题四 最短路
- [kuangbin带你飞]专题四 最短路练习 P HDU 4725
- [kuangbin带你飞]专题四 最短路练习D
- [kuangbin带你飞]专题四 最短路练习H,I,J
- acm_最短路
- Kuangbin专题四 最短路