HDU 2066 dijsktra + priority queue optimization

Although Cao'er is a road idiot (that is, someone who has stayed in Hangdian for more than a year, but still gets lost in the campus, Khan~), but Cao'er still likes to travel, because she will meet many people during the journey (Prince Charming, ^0^), a lot of things, but also enriching her own experience and watching beautiful scenery... Cao'er wants to go to many places, she wants to go to Tokyo Tower to see the night view, go to Venice to watch movies, and go to Yangmingshan to see the sea Taro, go to New York just to watch the snow scene, go to Paris to drink coffee and write a letter, and go to Beijing to visit Meng Jiangnu... As the winter vacation is coming soon, such a long time cannot be wasted, you must give yourself a good vacation, but also You can't waste training, so Cao'er decided to go to a place she wants to go in the shortest possible time! Because Cao'er's home is in a small town, there is no train passing by, so she can only go to the neighboring city to take the train (so pitiful~).
Input There are multiple groups of input data. The first line of each group is three integers T, S and D, indicating that there are T roads, S in the city adjacent to Cao'er's house, and D where Cao'er wants to go Then there are T lines ,  
each line has three integers a, b, time, indicating that the drive between cities a and b is time hours; (1=<(a,b)<=1000; possible between a,b There are multiple roads)  
The next line T+1 has S numbers, indicating the city connected to Cao'er's house;  
the next line T+2 has D numbers, indicating the place Cao'er wants to go. Output outputs the shortest time that grass can go to a favorite city. Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
 
 

9


Almost naked dijsktra, we just need to regard the neighboring towns as grass's home, and then find the minimum value.

I didn't write nu==mu return nu<mu WA when I reloaded the priority queue before.
Let's see the code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
using namespace std;
const int INF= 1e9;
struct node
{
    int u,v;
    node(int gg,int hh):u(gg),v(hh) {};
    bool friend operator < (const node &n,const node &m)
    {
        if(n.u==m.u) return n.u<m.u;
        return n.v>m.v;
    }
};
vector< node > p[1010];
int di[1010],g[1010],dis[1010],vis[1010];
void init(int n)
{
    int i;
    for(i=0; i<=1000; i++)
    {
        dis[i]=INF;
        vis[i]=false;
    }
}
void dijskra(int start,int n)
{
    init(n);
    priority_queue<node> q;
    dis[start]=0;
    q.push(node(start,0));
    while(!q.empty())
    {
        node temp=q.top();
        q.pop();
        int x=temp.u;
        if(vis[x]) continue;
        vis[x]=true;
        for(int i=0; i<p[x].size(); i++)
        {
            int y=p[x][i].u;
            if(vis[y]) continue;
            int d=p[x][i].v;
            if(dis[y]>dis[x]+d)
            {
                dis[y]=dis[x]+d;
                q.push(node(y,dis[y]));
            }
        }

    }

}
int main()
{
    int n,m,k;
    while(~scanf("%d %d %d",&n,&m,&k))
    {
        int i,a,b,c;
        for(i=0; i<n; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            p[a].push_back(node(b,c));
            p[b].push_back(node(a,c));
        }
        for(i=0; i<m; i++)
            scanf("%d",&g[i]);
        for(i=0; i<k; i++)
            scanf("%d",&di[i]);
        int minx=INF;
        for(i=0; i<m; i++)
        {
            dijskra(g[i],n);
            for(int j=0; j<k; j++)
            {
                if(minx>dis[di[j]])
                    minx=dis[di[j]];
            }
        }
        cout<<minx<<endl;
        for(i=0; i<1000; i++)
            p[i].clear();
    }

}
Today, I wrote a lot of dijsktra template questions one after another. Practice makes perfect

Guess you like

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