HDU 2544 shortest path (adjacency list + priority queue + dijstra optimization template)

shortest path

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 81706    Accepted Submission(s): 35385


Problem Description
In the annual school competition, all the finalists will receive a beautiful t-shirt. But every time our staff transports hundreds of clothes from the store back to the arena, it's very tiring! So now they want to find the shortest route from the store to the arena, can you help them?

 

 

Input
The input includes multiple sets of data. The first line of each set of data is two integers N and M (N<=100, M<=10000). N represents how many intersections there are on the streets of Chengdu. The intersection marked with 1 is where the store is located, and the intersection marked with N It is the location of the stadium, and M means that there are several roads in Chengdu. N=M=0 means the end of input. Next M lines, each line includes 3 integers A, B, C (1<=A, B<=N, 1<=C<=1000), indicating that there is a road between intersection A and intersection B, we The staff takes C minutes to walk this way.
Enter a route that guarantees there is at least 1 store to the arena.
 

 

Output
For each set of inputs, output a line representing the minimum time for a worker to walk from the store to the arena
 

 

Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

 

Sample Output
3 2
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1874  1217  1142  1385  2680 
 
Get simple questions and practice dijstra optimization
#include <iostream>
#include <cmath>
#include <iomanip>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
bool book[105];
struct node
{
    int c;
    int v;
    int nxt;
}e[20005];
struct node1
{
    int c;
    int v;
    friend bool operator <(node1 x, node1 y)
    {
        return xc > yc; // The smaller the member of x, the higher the priority, the more at the top of the queue 
    }
}a[20005];

int cnt=1;
int head[105];
priority_queue<node1>q;
void add(int u,int v,int z)
{
     e [cnt] .v = v;
     e[cnt].c = z;
     e [cnt] .nxt = head [u];
     head[u]=cnt++;
}
int d[105];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        while(!q.empty()) q.pop();
        memset(head,-1,sizeof(head));
        memset(d,inf,sizeof(d));
        memset(book,0,sizeof(book));
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            cin>>x>>y>>z;
            add(x,y,z);
            add(y,x,z);
        }
        for(int i=head[1];i!=-1;i=e[i].nxt)
        {
            node1 x;
            x.c=e[i].c;
            xv = e [i] .v;
            d[x.v]=x.c;
            book[x.v]=1;
            q.push(x);
        }
        while(!q.empty())
        {
            node1 x;
            x=q.top();
            book[x.v]=0;
            q.pop();
            for(int i=head[x.v];i!=-1;i=e[i].nxt)
            {
                if(d[e[i].v]>d[x.v]+e[i].c)
                {
                    d[e[i].v]=d[x.v]+e[i].c;
                    node1 and;
                    if(!book[e[i].v])
                    {
                        book [e [i] .v] = 1 ;
                        yv = e [i] .v;
                        y.c=d[e[i].v];
                        q.push(y);
                    }
                    
                }
            }
        }
        cout<<d[n]<<endl;
    }
    return 0;
}

            

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325136080&siteId=291194637
Recommended