Can't Save My Food--Nanjing Online Game L Magical Girl Haze

Address: https://nanti.jisuanke.com/t/31001

Vegetables... The test is the shortest path in layers, template questions, but I haven't seen...
The shortest path of hierarchical graph is to solve the shortest path problem on the graph that can be hierarchical graph.
The general model is:
On the graph, there are k chances to go directly through one side and ask the shortest path between the starting point and the end point.

Train of thought:
We set up dis[i][k] to indicate that we went to point I and passed through the shortest path of k edges free of charge.
For the end we currently find, we try to update the state of the starting point, do not select the free state of this side and choose the free state of this side, and then press these two states into the queue to update other states that can be reached.

DIGESTERA ALGORITHM WITH PRIORITY QUEUE

Note: When t-group data, it is important to remember to empty the vector array and be killed by this pit.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 100005;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

int n,m,k;
vector<pair<int,LL> >ve[N];
LL dis[N][15];
bool vis[N][15];

typedef struct Node{
    int a,b;
    LL dis;
    friend bool operator < (const Node &p,const Node &q)
    {
        return p.dis > q.dis;
    }
}Node;

void dijistra()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    priority_queue<Node>que;
    dis[1][0] = 0;
    que.push(Node{1,0,0});
    while(!que.empty())
    {
        Node t = que.top();
        que.pop();
        //cout << t.a << " " << t.b << endl;
        if(vis[t.a][t.b]) continue;
        vis[t.a][t.b] = true;
        int len = ve[t.a].size();
        for(int i = 0;i < len;++i)
        {
            int x = ve[t.a][i].fi;
            if(dis[x][t.b] > t.dis + ve[t.a][i].se){
                dis[x][t.b] = t.dis + ve[t.a][i].se;
                que.push(Node{x,t.b,dis[x][t.b]});
            }
            if(t.b + 1 <= k){
                if(dis[x][t.b + 1] > t.dis){
                    dis[x][t.b + 1] = t.dis;
                    que.push(Node{x,t.b + 1,dis[x][t.b + 1]});
                }
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&k);
        for(int i = 1;i <= n;++i)
            ve[i].clear();
        for(int i = 0;i < m;++i)
        {
            int u,v;
            LL c;
            scanf("%d %d %lld",&u,&v,&c);
            ve[u].pb(mp(v,c));
        }
        dijistra();
//        for(int i = 0;i <= k;++i)
//        {
//            for(int j = 1;j <= n;++j)
//            {
//                cout << dis[j][i] << " ";
//            }
//            cout << endl;
//        }
        printf("%lld\n",dis[n][k]);
    }
    return 0;
}

spfa with priority queue does not timeout, it does not need to timeout

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 100005;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

int n,m,k;
vector<pair<int,int> >ve[N];
int dis[N][15],vis[N][15];

typedef struct Node{
    int a,b,dis;
    friend bool operator < (const Node &p,const Node &q)
    {
        return p.dis > q.dis;
    }
}Node;
priority_queue<Node>que;

void spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    vis[1][0] = true;
    dis[1][0] = 0;
    que.push(Node{1,0,0});
    while(!que.empty())
    {
        Node t = que.top();
        que.pop();
        vis[t.a][t.b] = false;
        int len = ve[t.a].size();
        for(int i = 0;i < len;++i)
        {
            int x = ve[t.a][i].fi;
            if(dis[x][t.b] > dis[t.a][t.b] + ve[t.a][i].se){
                dis[x][t.b] = dis[t.a][t.b] + ve[t.a][i].se;
                if(!vis[x][t.b]){
                    vis[x][t.b] = true;
                    que.push(Node{x,t.b,dis[x][t.b]});
                }
            }
            if(t.b + 1 <= k){
                if(dis[x][t.b + 1] > dis[t.a][t.b] ){
                    dis[x][t.b + 1] = dis[t.a][t.b] ;
                    if(!vis[x][t.b + 1]){
                        vis[x][t.b + 1] = true;
                        que.push(Node{x,t.b + 1,dis[x][t.b + 1]});
                    }
                }
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&k);
        for(int i = 1;i <= n;++i)
            ve[i].clear();
        for(int i = 0;i < m;++i)
        {
            int u,v,c;
            scanf("%d %d %d",&u,&v,&c);
            ve[u].pb(mp(v,c));
        }
        spfa();
        printf("%d\n",dis[n][k]);
    }
    return 0;
}

Posted by landonmkelsey on Sun, 03 Feb 2019 14:09:18 -0800