Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
-
Line 1: Two integers: T and N
-
Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.
Output
- Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
题目大意:
给定T条双向路和n个点,求1号点到n号点的最短路。
核心思想:
Dijkstra+优先队列优化。
以下代码分别采用了两种存图方式,邻接表和vector。
邻接表存图:
#include<cstdio>
#include<iostream>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e4+10,M=2e6+10;
int head[N],dis[N],cb;
bool vis[N];
struct node
{
int y,v,ne;
} side[M];
struct Node
{
int y,v;
Node()
{
}
Node(int yy,int vv)
{
y=yy;
v=vv;
}
bool operator<(const Node oth)const
{
return v>oth.v;
}
};
void init()
{
cb=0;
for(int i=0; i<N; i++)
head[i]=-1;
return;
}
//邻接表添加边函数
void add(int x,int y,int v)
{
side[cb].y=y;
side[cb].v=v;
side[cb].ne=head[x];
head[x]=cb++;
return;
}
void dij(int st)
{
for(int i=0; i<N; i++)
{
vis[i]=0;
dis[i]=inf;
}
dis[st]=0;
priority_queue<Node>q;
q.push(Node(st,0));
while(!q.empty())
{
int te=q.top().y;
q.pop();
if(vis[te])continue;
vis[te]=1;
for(int i=head[te]; i!=-1; i=side[i].ne)
{
int ty=side[i].y;
int tv=side[i].v;
if(dis[ty]>dis[te]+tv)
{
dis[ty]=dis[te]+tv;
q.push(Node(ty,dis[ty]));
}
}
}
return;
}
int main()
{
int m,n,x,y,v;
init();
//输入
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&x,&y,&v);
add(x,y,v);
add(y,x,v);
}
//dij
dij(1);
//输出
printf("%d\n",dis[n]);
return 0;
}
vector存图:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e3+10;
int dis[N];
bool vis[N];
struct node
{
int y,w;
node()
{
}
node(int yy,int ww)
{
y=yy;
w=ww;
}
};
struct Node
{
int y,w;
Node()
{
}
Node(int yy,int ww)
{
y=yy;
w=ww;
}
bool operator<(const Node oth)const
{
return w>oth.w;
}
};
vector<node>G[N];
priority_queue<Node>q;
void dij(int st)
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[st]=0;
q.push(Node(st,0));
while(!q.empty())
{
int te=q.top().y;
q.pop();
if(vis[te])continue;
vis[te]=1;
int len=G[te].size();
for(int i=0; i<len; i++)
{
int ty=G[te][i].y;
int tw=G[te][i].w;
if(dis[ty]>dis[te]+tw)
{
dis[ty]=dis[te]+tw;
q.push(Node(ty,dis[ty]));
}
}
}
return;
}
int main()
{
int n,m,x,y,w;
//输入
cin>>m>>n;
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&x,&y,&w);
G[x].push_back(node(y,w));
G[y].push_back(node(x,w));
}
//dij
dij(1);
//输出
printf("%d\n",dis[n]);
return 0;
}