$ Luogu $ $ P2939 $ $ [USACO09FEB] $ Remaking $ Revamping $ $ Trails $

link

background

\ (USACO \) \ (2009 \) \ (On Feb \) \ (T3 \) , \ (Luogu \) \ (P2939 / BZOJ1579 \)

The meaning of problems

Given \ (n-\) points, \ (m \) two end edges of the \ (X, Y \) , the path may be such that a predetermined maximum of \ (K \) edges becomes \ (0 \) . Seeking \ (1 \) to the \ (n-\) of the shortest path length.

solution

Figure layered shortest template.
Since the free \ (k \) times, built \ (k \) layer diagram (assuming that becomes \ (0 \) , the more the higher the number of edges in the graph) can be.
Note that each edge connected again at each level, and \ (X \) to a higher-level \ (Y \) connected \ (0 \) side, \ (Y \) to a higher-level \ (X \ ) even \ (0 \) side.
From \ (1 \) No. o'clock \ (Dijkstra \) , the answer is first of all layers \ (n-\) Shortest Path minimum points.

detail

Please be sure to pay attention to check the map topic array bloom big enough . Make sure sure sure substituted into the limit data validation .

Code

\(View\) \(Code\)

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
const int inf=0x3f3f3f3f;
int n,m,k,s,t,u,v,w;
int dis[220005],ans=inf;
int num,head[220005];
bool vis[220005];
struct edge
{
    int ver,nxt,w;
}e[4100005];
inline void adde(int u,int v,int w)
{
    e[++num].ver=v;
    e[num].w=w;
    e[num].nxt=head[u];
    head[u]=num;
}
void dijkstra(int s)
{
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    priority_queue<pair<int,int> > q;
    dis[s]=0;
    q.push(make_pair(0,s));
    while(!q.empty())
    {
        int x=q.top().second;
        q.pop();
        if(vis[x])
            continue;
        vis[x]=1;
        for(register int i=head[x];i;i=e[i].nxt)
        {
            int y=e[i].ver,w=e[i].w;
            if(dis[x]+w<dis[y])
            {
                dis[y]=dis[x]+w;
                q.push(make_pair(-dis[y],y));
            }
        }
    }
}
int main()
{
    n=read();
    m=read();
    k=read();
    s=1;
    t=n;
    for(register int i=1;i<=m;i++)
    {
        u=read();
        v=read();
        w=read();
        adde(u,v,w);
        adde(v,u,w);
        for(register int j=1;j<=k;j++)
        {
            adde(u+(j-1)*n,v+j*n,0);
            adde(v+(j-1)*n,u+j*n,0);
            adde(u+j*n,v+j*n,w);
            adde(v+j*n,u+j*n,w);
        }
    }
    dijkstra(s);
    for(register int i=0;i<=k;i++)
        ans=min(ans,dis[t+i*n]);
    if(ans<inf)
        printf("%d\n",ans);
    else
        printf("-1\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11846010.html