#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<climits>
using namespace std;
const int maxn=200;
const int INF=INT_MAX;
struct edge
{
int to;
int length;
edge(int t,int l)
:to(t),length(l)
{
}
};
struct Point
{
int number;
int distance;
Point(int n,int d)
:number(n),distance(d){}
bool operator<(const Point &p)const{
return distance>p.distance;
}
};
vector<edge>graph[maxn];
int dis[maxn];
void dijkstra(int s)
{
priority_queue<Point>mypriorityqueue;
dis[s]=0;
mypriorityqueue.push(Point(s,dis[s]));
while(!mypriorityqueue.empty())
{
int u=mypriorityqueue.top().number;
mypriorityqueue.pop();
for(int i=0;i<graph[u].size();++i)
{
int v=graph[u][i].to;
int d=graph[u][i].length;
if(dis[v]>dis[u]+d)
{
dis[v]=dis[u]+d;
mypriorityqueue.push(Point(v,dis[v]));
}
}
}
return;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(graph,0,sizeof(graph));
fill(dis,dis+n,INF);
while(m--!=0)
{
int from,to,length;
scanf("%d%d%d",&from,&to,&length);
graph[from].push_back(edge(to,length));
graph[to].push_back(edge(from,length));
}
int s,t;
scanf("%d%d",&s,&t);
dijkstra(s);
if(dis[t]==INF)
{
dis[t]=-1;
}
printf("%d\n",dis[t]);
}
return 0;
}
畅通工程续--最短路径
最新推荐文章于 2021-04-08 22:11:29 发布