题目:
codeforces : http://codeforces.com/problemset/problem/20/C
大意:
给n和m,n是图中的n个节点,m是图中的路径数,求到1到n的最短路径,如果1无法到达n,输出-1。
思路:
c++的vector真是太好用了,看了巫泽俊(watashi)神犇犇的代码比做十道题还的收获还大。
用一个数组来记录到达该点的前一个点,然后回溯。
AC代码:
#include<cstdio>
#include<queue>
#include<utility>
#include<vector>
using namespace std;
const int M = 1e5 + 10;
const long long INF = 1e18;
int p[M];
long long d[M];
vector<pair<int, int > > e[M];
priority_queue<pair<long long, long long> >pq;
vector<pair<int, int> >::iterator it;
void dump(int a)//向前回溯,1前是0,所以a要大于1
{
if(a > 1)
dump(p[a]);
printf("%d ", a);
}
main()
{
int n, m;
scanf("%d %d", &n, &m);
while(m--)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
e[a].push_back(make_pair(b, c));
e[b].push_back(make_pair(a, c));
}
fill(d, d + n + 1, INF);
d[1] = 0;
pq.push(make_pair(0, 1));
while(!pq.empty())
{
long long a = -pq.top().first, b = pq.top().second;
pq.pop();
if(a != d[b])
continue;
for(it = e[b].begin(); it != e[b].end(); it++)
{
if(it ->second + a < d[it ->first])
{
d[it ->first] = it->second + a;
p[it ->first] = b;
pq.push(make_pair(-d[it->first], it->first));
}
}
}
if(d[n] == INF)
puts("-1");
else
dump(n);
}