Code Forces 26C Dijkstra?

C. Dijkstra?
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form aibi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.

It is possible that the graph has loops and multiple edges between pair of vertices.

Output

Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.

Examples
input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
1 4 3 5 
input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
1 4 3 5 
 
          
在Dijstra上优化一下,就是用优先队列去找最小的值
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
const long long int INF=(long long int )1<<60;
#define MAX 100000
vector< pair > a[MAX+5];
struct Node
{
    int pos;
    int value;
    Node(){};
    Node(int pos,int value)
    {
        this->pos=pos;
        this->value=value;
    }
    friend bool operator <(Node a,Node b)
    {
        return a.value>b.value;
    }
};
priority_queueq;
int vis[MAX+5];
long long int d[MAX+5];
int pre[MAX+5];
int n,m;
void Dijstra()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        d[i]=INF;
    pre[1]=-1;
    d[1]=0;
    q.push(Node(1,0));
    while(!q.empty())
    {
        Node term=q.top();
        q.pop();
        if(vis[term.pos]) continue;
        vis[term.pos]=1;

        for(int i=0;i




你可能感兴趣的:(Code,Forces,26C,Dijskra,图论,CF,最短路-Dijkstra)