More than 2019 cattle off summer school camp (fourth) J-free

> Portal <

Meaning of the questions: to give you n cities, m road, after the cost of each one takes this road, now give you k a chance, make the most k cost of the road is 0, ask from the starting point s to the end t spend minimum costs

Ideas: a hierarchical view of the most classic naked short-circuit problem

method one

Code

#include <bits / STDC ++ H.>
 the using  namespace STD;
 struct Edge { int to, cost;}; 
typedef pair < int , int > P; // First is the shortest distance, second vertex number 

const  int max_v = 2000005 ;
 int n-, m, S, T, K;
 int D [max_v]; 
Vector <Edge> G [max_v]; 

void Dijkstra () 
{ 
    The priority_queue <P, Vector <P>, Greater <P>> que; 
    Memset (D , 0x3F , the sizeof (D)); 
    D [S] =0;
    que.push(P(0, s));
    
    while (!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &s, &t, &k);
    for(int i = 1; i <= m; i++) {
        int u, v, cost;
        scanf("%d%d%d", &u, &v, &cost);
        for(int j = 0; j <= k; j++) {
            G[u+n*j].push_back({v+n*j,cost});
            G[v+n*j].push_back({u+n*j,cost});
            if(j < k) {
                G[u+n*j].push_back({v+n*(j+1),0});
                G[v+n*j].push_back({u+n*(j+1),0});
            }
        }
    }
    dijkstra();
    //int ans=0x3f3f3f3f;
    //for(int i=0;i<=k;i++) ans=min(ans,dis[t+n*i]);
    printf("%d\n", d[t+k*n]);
    return 0;
}
View Code

 

Method Two

So I am most surprised that this question some people did not go through the stack of Dijkstra's algorithm optimized direct violence out? ? ? Then a look n, m is less than 1E3, 1e6 complexity at most, also not critical, okay. Of course, it also uses optimized algorithms or SPFA, tomorrow another look

Code

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int MAX_N=1005;
int n, m, s, t, k;
vector<P> G[MAX_N];
int d[MAX_N][MAX_N];

void dijkstra()
{
    memset(d, 0x3f, sizeof(d));
    d[s][0]=0;
    queue<P> que; 
    que.push({s,0});
    
    while(!que.empty()) {
        int u = que.front().first, t = que.front().second; que.pop();
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].first, cost = G[u][i].second;
            if(d[v][t]>d[u][t]+cost) d[v][t] = d[u][t]+cost, que.push({v,t});
            if(t<k&&d[v][t+1]>d[u][t]) d[v][t+1] = d[u][t], que.push({v,t+1});
        }
    }
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &s, &t, &k);
    for(int i=0;i<m;i++) {
        int u, v, cost;
        scanf("%d%d%d", &u, &v, &cost);
        G[u].push_back({v,cost});
        G[v].push_back({u,cost});
    }
    dijkstra();
    printf("%d\n",d[t][k]);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11261270.html