求助大佬们,我调了一晚上了,还是73分,3、4个TLE,8个WA,感谢大佬
查看原帖
求助大佬们,我调了一晚上了,还是73分,3、4个TLE,8个WA,感谢大佬
719909
Love_lsla楼主2023/9/5 21:42
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;

typedef pair<int,int> PII;
const int N = 10010*10,M = 50010*20;
int n,m,k;
int h[N],e[M],ne[M],w[M],idx;
int dist[N*2];
int start,ed;
int st[N];

void add(int a,int b,int c)
{
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx++;
}

void dijkstra()
{
    priority_queue<PII,vector<PII>,greater<PII>> heap;
    memset(dist,0x3f,sizeof dist);
    dist[start] = 0;
    heap.push({dist[start],start});

    while(!heap.empty())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second,distance = t.first;

        if(st[ver]) continue;
        st[ver] = true;

        for(int i = h[ver];i!=-1;i=ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j],j});
            }
        }
    }
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&start,&ed);
    
    memset(h,-1,sizeof h);
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);//先将原图的边链接
        for(int j = 1;j<=k;j++)//建立分层图
        {
            //建立当前层到下一层的边权为0的边
            add(a+(j-1)*n,b+j*n,0);
            add(b+n*(j-1),a+n*j,0);
            //建立下一层的边
            add(a+n*j,b+n*j,c);
            add(b+n*j,a+n*j,c);
        }
    }

    dijkstra();
    int ans = 0x3f3f3f3f;
    for(int i = 0;i<=k;i++) ans = min(ans,dist[ed + i * n]);
    cout<<ans<<endl;

    return 0;
}
2023/9/5 21:42
加载中...