1 solutions

  • 0
    @ 2023-12-3 21:35:25

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    const int maxn(5005);
    const int maxe(100010);
    const int inf(100000000);
    struct Edge{
        int v, w, next;
    }e[maxe*2];
    int head[maxn], cnt;
    int n, m;
    int s, t, k;
    
    void init(){
        memset(head, -1, sizeof head);
        cnt = 0;
    }
    void add_Edge(int u, int v, int w){
        e[cnt].v = v, e[cnt].w = w, e[cnt].next = head[u];
        head[u] = cnt ++;
    }
    struct Point{
        int u, w, c;
        Point(int x, int y, int z) : u(x), w(y), c(z){}
        //friend bool operator < (const Point &a, const Point &b){
        //    return a.w > b.w;
        //}
    };
    int dist[maxn][55];
    int vis[maxn][55];
    
    void spfa(){
        queue < Point > q;
        memset(vis, 0, sizeof vis);
        for(int i = 0; i <= n; i ++)
          for(int j = 0; j <= 60; j ++)
            dist[i][j] = inf;
        vis[s][0] = 1;
        dist[s][0] = 0;
        q.push(Point(s, 0, 0));
        while(!q.empty()){
            Point tmp = q.front(); q.pop();
            int u = tmp.u;
            int w = tmp.w;
            int c = tmp.c;
            vis[u][c] = 0;
            int cc = c;
            for(int i = head[u]; i + 1; i = e[i].next){
                int v = e[i].v;
                int cost = dist[u][cc] + e[i].w;
                if(cc + 1 > k )
                  c = k;
                else
                  c = cc + 1;
                if(dist[v][c] > cost){
                    dist[v][c] = cost;
                    if(!vis[v][c]){
                        vis[v][c] = 1;
                        q.push(Point(v, dist[v][c], c));
                    }
                }
            }
        }
        if(dist[t][k] == inf) puts("-1");
        else
          printf("%d\n", dist[t][k]);
    }
    void input(){
        init();
        for(int i = 0; i < m; i ++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add_Edge(u, v, w);
            add_Edge(v, u, w);
        }
        scanf("%d%d%d", &s, &t, &k);
        k = k/10 + (k%10 != 0);
    }
    int main(){
    	//freopen("shortest.in","r",stdin);
    	//freopen("shortest.out","w",stdout);
        while(~scanf("%d%d", &n, &m)){
            input();
            spfa();
        }
        return 0;
    }
    
    //
    //#include <iostream>
    //#include <cstdio>
    //#include <cstring>
    //#include <algorithm>
    //#include <cstdlib>
    //#include <ctime>
    //
    //typedef unsigned long long u64;
    //u64 random(u64 n)
    //{
    //	u64 t;
    //	if(n<=RAND_MAX)
    //	{
    //		t = rand();
    //		return t%n;
    //	}
    //	else
    //	{
    //		return rand()+random(n/(RAND_MAX+1))*(RAND_MAX+1);
    //	}
    //}
    //
    //int main()
    //{
    //	freopen("shortest.in","w",stdout);
    //	srand(time(NULL));
    //	printf("4 4\n1 2 1\n2 3 2\n1 3 100\n3 4 1\n1 3 50\n");
    //	for(int i = 0;i < 50; i++)
    //	{
    //		int N = rand()%35+1;
    //		int M = rand()%85+1;
    //		printf("%d %d\n",N,M);
    //		for(int j = 0 ;j < M;j++)
    //		{
    //			int A = rand()%35+1;
    //			int B = rand()%35+1;
    //			int C = rand()%85+1;
    //			printf("%d %d %d\n",A,B,C);
    //		}
    //		int S = rand()%85+1;
    //		int T = rand()%35+1;
    //		int K = rand()%35+1;
    //		printf("%d %d %d\n",S,T,K);
    //	}
    //	return 0;
    //}
    
    • 1

    Information

    ID
    1324
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By