HDU 2851 Dijkstra algorithm variants

Source: Internet
Author: User

Dijkstra algorithm Template:

int visited[i]//node I if accessed is 1, there is no 0Int dist[i]//current node J to the other nodes of the shortest length int w[i][j]//edge (i,j) of the Weight value initialization: (node 1~n) memset (v,0,sizeof (v)); Dist[j]=0;dist[i]=inf; (i>=1&&i<=n&&i!=j)    cin>>a>>b>>x;     if (w[a][b]>x)//a to B may have more than one path    w[b][a]=w[a][b]=x;w[x][y]=inf; the Edge (x, y) does not exist for (i=1;i<=n;i++) {    int x,m=inf;    for (j=1;j<=n;j++)        if (!visited[j]&&dist[j]<m)        {            m=dist[j];            x=j;        }    Visited[x]=1;for (j=1;j<=n;j++) If (!visited[j])        dist[j]=min (Dist[j],dist[x]+w[x][j])}

Description

Lode Runner is a famous game, I think you played in your childhood.


Ok, now, I simple the problem. In the game, it had N horizontal roads, the ladders always stay at right side vertically, and the ladders is extending to Wards up and down with unlimited length. If ladder near or cross a road, little Wiskey would walk on the road by climbed the ladder. Roads were covered by each of the other in the x-axis; It'll be considered can passing through. Each road has a integer W means the dangerous level.

Little Wiskey must start at the most road, and he can ' t go back, he always go ahead. Wiskey want to go some roads, so he needs to know how minimum sum of dangerous would happen. You can finish the game, yes?

Input

The first integer C represents the number of cases, and C cases followed.

Each test case is contains a single integer N roads (1<=n<=) and M destinations (1<=m<=500). The next N line contains three integers Si, Ei, Wi, meaning the road build from Si to Ei, and the Wi dangerous level (0 &L t;= Si <= Ei <=, 1 <= Wi <= 1000). And the roads sorted by Ei increasing yet. The last M line contains integers mean little wiskey ' s destinations.

Output


For each questions, output the minimum sum of dangerous. If can ' t reach the goal, please print-1.

Sample Input

Sample Output




Turn the weight of a point into the weight of the edge that leads to it

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <string    > #include <algorithm> #define LL long long#define inf 0x3f3f3f3fusing namespace std;struct node{int l,r; int W;}    Q[2010];bool visited[2010];int dist[2010];int wx[2010][2010];int main () {int T;    cin>>t;        while (t--) {int n,m;        cin>>n>>m;        for (int i=1;i<=n;i++) scanf ("%d%d%d", &AMP;Q[I].L,&AMP;Q[I].R,&AMP;Q[I].W);        memset (wx,inf,sizeof (WX));        for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (i==j) wx[i][j]=wx[j][i]=0; for (int i=2;i<=n;i++) for (int j=1;j<=i-1;j++) if (Q[J].R&GT;=Q[I].L) wx[    J][I]=Q[I].W;    memset (visited,0,sizeof (visited));    memset (dist,inf,sizeof (Dist));    dist[1]=0;        for (int i=1;i<=n;i++) {int x,mx=inf; for (int j=1;j<=n;j++) if (!visited[j]&&dist[j]&LT;MX) {mx=dist[j];        X=j;        } visited[x]=1;    for (int j=1;j<=n;j++) dist[j]=min (Dist[j],dist[x]+wx[x][j]);            } while (m--) {int b;            cin>>b;            if (Dist[b]==inf) cout<<-1<<endl;        else cout<<dist[b]+q[1].w<<endl; }} return 0;}


HDU 2851 Dijkstra algorithm variants

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.