分层最短路-例题

题目链接1 - - codeforces gym 101873C
题目链接2–codeforces gym 102501A
题目链接3 洛谷 飞行路线

读懂了题之后就是板子题了,一般就是一维的迪杰斯特拉在开一维记录状态
比如 dist[i][j]记录为 到达i点用了j次机会点最短路/权值
或者到达i点路径长度为j的最小花费

Gym 101873C

有个需要注意点地方就是 可以呆在 一个点不动,不是每一步都需要移动

#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <vector>


using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
const ll mod = 1000000007;

#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)

ll read() {
    
    
    ll x = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9')ch = getchar();
    while(ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x;
}

void out(ll x) {
    
    
    int stackk[40];
    if (x < 0) {
    
    
        putchar('-');
        x = -x;
    }
    if (!x) {
    
    
        putchar('0');
        return;
    }
    int top = 0;
    while (x) stackk[++top] = x % 10, x /= 10;
    while (top) putchar(stackk[top--] + '0');
}
int x,n,m,head[maxn],cnt,t,dist[1002][1002],ci;
struct node {
    
    
    int u,v,w,next,t,p;
} e[maxn];
node c[maxn];
struct Node {
    
    
    int dian,t,p;
    friend  bool operator < (const Node &x,const Node &y) {
    
    
        if(x.p!=y.p)return x.p>y.p;
        else return x.t>y.t;
    }
    Node() {
    
    };
    Node(int a,int b,int c) {
    
    
        dian=a;
        t=b;
        p=c;
    }
};
void add(int u,int v) {
    
    
    e[cnt].u=u,e[cnt].v=v;
    e[cnt].next=head[u],head[u]=cnt++;
}
void D() {
    
    
    dist[1][c[1].t]=c[1].p;
    priority_queue<Node>q;
    q.push(Node(1,c[1].t,c[1].p));
    while(q.size()) {
    
    
        ci++;
        Node fr= q.top();
        q.pop();
        int dian=fr.dian;
        int ti=fr.t;
        if(ci>100000) break;
        if(dist[1][x]!=inf) break;
          if(dist[dian][ti+c[dian].t]>dist[dian][ti]+c[dian].p) {
    
    
               dist[dian][ti+c[dian].t]=dist[dian][ti]+c[dian].p;
                q.push(Node(dian,ti+c[dian].t,dist[dian][ti+c[dian].t]));
            }
        for(int i=head[dian]; ~i; i=e[i].next) {
    
    
            int v=e[i].v;
            if(ti+t+c[v].t>x) continue;
            if(dist[v][ti+t+c[v].t]>dist[dian][ti]+c[v].p) {
    
    
                dist[v][ti+t+c[v].t]=dist[dian][ti]+c[v].p;
                q.push(Node(v,ti+t+c[v].t,dist[v][ti+t+c[v].t]));
            }

        }
    }
}
int main() {
    
    
    mst(head,-1);
    rep(i,0,1000) rep(j,0,1000) dist[i][j]=inf;
    x=read(),n=read(),m=read(),t=read();
    for(int i=1 ; i<=m ; i++) {
    
    
        int u=read();
        int v=read();
        add(u,v),add(v,u);
    }
    for(int i=1 ; i<=n ; i++) {
    
    
        c[i].t=read();
        c[i].p=read();
    }
    D();
    if(dist[1][x]>=inf) puts("It is a trap.");
    else printf("%d\n",dist[1][x]);
    return 0;
}
/**

**/

Gym 102501A

#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <vector>
 
 
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
const ll mod = 1000000007;
 
#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
 
ll read() {
    
    
    ll x = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9')ch = getchar();
    while(ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x;
}
 
void out(ll x) {
    
    
    int stackk[40];
    if (x < 0) {
    
    
        putchar('-');
        x = -x;
    }
    if (!x) {
    
    
        putchar('0');
        return;
    }
    int top = 0;
    while (x) stackk[++top] = x % 10, x /= 10;
    while (top) putchar(stackk[top--] + '0');
}
 
int sx,sy,ex,ey,t,c[maxn],n,b;
int head[maxn],cnt,dist[1010][1005],vis[1010][1005];
struct node {
    
    
    int x,y,u,v,w,next;
} p[maxn];
node e[maxn];
struct Node {
    
    
    int val,dian,dis;
    friend bool operator < (const Node &x,const Node &y) {
    
    
        if(x.val==y.val) return x.dis>y.dis;
       else  return x.val>y.val;
    }
    Node(){
    
    }
    Node(int x,int y,int z){
    
    
        val=x,dian=y,dis=z;
    }
};
 
void add(int u,int v,int w) {
    
    
    e[cnt].u=u,e[cnt].v=v;
    e[cnt].w=w,e[cnt].next=head[u];
    head[u]=cnt++;
}
 
int cal(int id1,int  id2) {
    
    
    int   x1=p[id1].x;
    int   x2=p[id2].x;
    int   y1=p[id1].y;
    int   y2=p[id2].y;
    int    temp=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return ceil(sqrt(temp));
}
 
void D() {
    
    
    priority_queue<Node>q;
    dist[0][0]=0;
    q.push(Node(0,0,0));
    // val  dian  dis
    while(q.size())
    {
    
    
        Node fr=q.top();
        q.pop();
        int dian=fr.dian;
        int dis=fr.dis;
       if(vis[dian][dis]) continue;
        vis[dian][dis]=1;
        for(int i=head[dian];~i;i=e[i].next)
        {
    
    
            int v=e[i].v;
            int juli=cal(dian,v);
            int pri = juli*c[e[i].w];
            if(juli+dis>b) continue;
            if(dist[v][juli+dis]>dist[dian][dis]+pri)
            {
    
    
               dist[v][juli+dis]=dist[dian][dis]+pri;
               q.push(Node(dist[v][juli+dis],v,juli+dis));
            }
        }
    }
}
 
int main() {
    
    
    mst(head,-1);
    sx=read(),sy=read(),ex=read(),ey=read(),b=read(),c[0]=read(),t=read();
    rep(i,1,t) c[i]=read();
    n=read();
    rep(i,0,1000) rep(j,0,4000) dist[i][j]=inf;
    p[0].x=sx,p[0].y=sy,p[n+1].x=ex,p[n+1].y=ey;
    add(0,n+1,0),add(n+1,0,0);
    for(int i=1 ; i<=n ; i++) {
    
    
        int x,y,l;
        cin>>x>>y>>l;
        p[i].x=x,p[i].y=y;
        add(0,i,0),add(n+1,i,0);
        add(i,0,0),add(i,n+1,0);
        for(int j=1 ; j<=l ; j++) {
    
    
            int xx,yy;
            cin>>xx>>yy;
            xx++;
            add(i,xx,yy),add(xx,i,yy);
        }
    }
    D();
    int ans=inf;
    for(int i=0 ;i<=b ;i++) ans=min(ans,dist[n+1][i]);
    if(ans==inf) puts("-1");
    else printf("%d\n",ans);
    return 0;
}
/**
1 1
10 2
12
100
2
10
50
3
2 3 2 1 1 2 2
5 5 1 2 1
9 3 0
**/

洛谷-飞行路线

#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <vector>


using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
const ll mod = 1000000007;

#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)

ll read() {
    
    
    ll x = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9')ch = getchar();
    while(ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    return x;
}

void out(ll x) {
    
    
    int stackk[40];
    if (x < 0) {
    
    
        putchar('-');
        x = -x;
    }
    if (!x) {
    
    
        putchar('0');
        return;
    }
    int top = 0;
    while (x) stackk[++top] = x % 10, x /= 10;
    while (top) putchar(stackk[top--] + '0');
}
int head[maxn],cnt,n,m,k,st,en,dist[maxn][11];
struct node {
    
    
    int u,v,w,next;
} e[maxn];
struct Node {
    
    
    int c,dis,dian;
    friend bool operator < (const Node &x,const Node &y) {
    
    
        return x.dis>y.dis;
    }
    Node(){
    
    };
    Node(int x,int y,int z){
    
    
        dian=x;
        c=y;
        dis=z;
    }

};
void add(int u,int v,int w) {
    
    
    e[cnt].u=u,e[cnt].v=v,e[cnt].w=w;
    e[cnt].next=head[u],head[u]=cnt++;
}
void D() {
    
    
    dist[st][0]=0;
    priority_queue<Node>q;
    q.push(Node(st,0,0));
    while(q.size())
    {
    
    
        Node fr = q.top();
        q.pop();
        int dian=fr.dian;
        int c=fr.c;
        int dis=fr.dis;

        for(int i=head[dian];~i;i=e[i].next)
        {
    
    
            int v=e[i].v;
            if(dist[v][c]>dist[dian][c]+e[i].w)
            {
    
    
                dist[v][c]=dist[dian][c]+e[i].w;
                q.push(Node(v,c,dist[v][c]));
            }
              if(dist[v][c+1]>dist[dian][c]&&c+1<=k)
            {
    
    
                dist[v][c+1]=dist[dian][c];
                q.push(Node(v,c+1,dist[v][c+1]));
            }

        }

    }



}
int main() {
    
    
    mst(head,-1);
    n=read(),m=read(),k=read();
    rep(i,0,n) rep(j,0,10) dist[i][j]=inf;
    st=read(),en=read();
    st++,en++;
    for(int i=1 ; i<=m ; i++) {
    
    
        int u=read();
        int v=read();
        int w=read();
        u++,v++;
        add(u,v,w),add(v,u,w);
    }
    D();
    int ans=inf;
    for(int i=0 ;i<=k ;i++) ans=min(ans,dist[en][i]);
    out(ans);
    return 0;
}
/**

**/

猜你喜欢

转载自blog.csdn.net/wmy0536/article/details/109519922
今日推荐