Nefu 1132 layer Diagram shortest circuit

Source: Internet
Author: User

Click to open link

Test instructions: Gives the start and end points, then a K-chance to halve the values of some paths on the path, asking the minimum cost from the start to the end

Idea: It is obvious that the level of the most short-circuit, and I did not study the meaning of the algorithm, teammates and I said is to divide the graph into N-layer, and then this can be halved the cost of connecting this n-layer, and then run a similar shortest can be, today changed a template, changed to their favorite style, I'm going to do a couple of layering charts tomorrow and look at the concept, this is a template problem.

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long Long ull;typedef pair<int,int> p;const int inf=0x3f3f3f3f;const ll inf=0x3f3f3f3f3f3f3f3fll;const int maxn=10010;    struct edge{int to,cost; Edge (int a,int b) {to=a;cost=b;}};    Vector<edge>g[maxn];int vis[maxn],dis[maxn],n,m,k;int Dijkstra (int s,int t) {memset (dis,inf,sizeof (dis));    memset (vis,0,sizeof (VIS));    Priority_queue<p, Vector<p>, greater<p> >que;    Dis[s]=0;que.push (P (0,s));        while (!que.empty ()) {P p=que.top (); Que.pop ();        int V=p.second;        if (Vis[v]) continue;        Vis[v]=1;            for (int i=0;i<g[v].size (); i++) {Edge e=g[v][i];                if (Dis[v]+e.cost<dis[e.to]) {dis[e.to]=dis[v]+e.cost;            Que.push (P (dis[e.to],e.to));       } }} int ans=inf;    for (int i=0;i<=k;i++) ans=min (ans,dis[i*n+t]);    if (Ans==inf) return-1; return ans;}    int U[maxn],v[maxn],cost[maxn],num[110][110];int Main () {int st,en;        while (scanf ("%d%d%d", &st,&en,&k)!=-1) {for (int i=0;i<maxn;i++) g[i].clear ();        st++;en++;        N=max (St,en);        scanf ("%d", &m);        memset (num,inf,sizeof (num));            for (int i=1;i<=m;i++) {scanf ("%d%d%d", &u[i],&v[i],&cost[i]); u[i]++;            V[i]++;num[u[i]][v[i]]=min (Cost[i],num[u[i]][v[i]);        N=max (N,max (u[i],v[i)); } for (int i=1;i<=m;i++) {for (int j=0;j<=k;j++) {G[j*n+u[i]].push_back (Edge (J*n+v[i)                , Num[u[i]][v[i]]);            if (j<k) G[j*n+u[i]].push_back (Edge ((j+1) *N+V[I],NUM[U[I]][V[I]]/2);        }} int Ans=dijkstra (st,en);        if (ans==-1) printf ("kengdie\n");    else printf ("%d\n", ans); } return 0;}

Nefu 1132 layer Diagram shortest circuit

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.