hdu--2851 (Shortest way)

Source: Internet
Author: User

A simple shortest-circuit ...

I went to the topic sample for a long time, the original 12345 those goals are said which road ah, pit ... Note that the weight here is not the edge of the point, the initial cost is accidentally written wrong, with the optimization of the DIJ should be faster ... I'm so tired.

Code:

#include <iostream> #include <cstring> #include <cstdio> #include <map> #include <cstring > #include <algorithm> #define INF 0x3f3f3f3f#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef Long long ll;typedef unsigned long long llu;const int Maxn=2000+10;bool vis[maxn];int lowcost[maxn],cost[maxn][maxn];int        n,m;struct node{int l,r,w;} pp[maxn];void dij (int beg) {for (int i=0; i<n; ++i) {lowcost[i]=inf;    Vis[i]=false;    } lowcost[beg]=0;        for (int j=0; j<n; ++j) {int k=-1;        int _min=inf; for (int i=0; i<n; ++i) if (!vis[i] && lowcost[i]<_min) {_min=lowcost[i                ];            K=i;        } if (k==-1) break;        Vis[k]=true;                for (int i=0; i<n; ++i) if (!vis[i] && lowcost[k]+cost[k][i]<lowcost[i]) {            Lowcost[i]=lowcost[k]+cost[k][i]; }}}int Main () {freopen ("1.txt"," R ", stdin);    int Kase;    scanf ("%d", &kase);        while (kase--) {scanf ("%d%d", &n,&m);        MEM (Cost,inf);        for (int i=0; i<n; ++i) {scanf ("%d%d%d", &AMP;PP[I].L,&AMP;PP[I].R,&AMP;PP[I].W); } for (int i=0, i<n; ++i) {for (int j=i+1; j<n; ++j) if (PP[I].R&LT;=PP[J].R &        amp;& pp[i].r>=pp[j].l) COST[I][J]=PP[J].W;        } dij (0);            for (int i=0; i<m; ++i) {int x;            scanf ("%d", &x);            if (lowcost[x-1]!=inf) printf ("%d\n", LOWCOST[X-1]+PP[0].W);        else printf ("%d\n",-1); }} return 0;}


hdu--2851 (Shortest way)

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.