社区讨论

关于奇怪のTLE

P4822[BJWC2012] 冻结参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lobymcn0
此快照首次捕获于
2023/10/30 05:04
2 年前
此快照最后确认于
2023/11/04 10:21
2 年前
查看原帖
rt
dijkstra堆优化,#1、2 & #4 ~ 10 都10ms跑完了,但#3却TLE了,有些神奇,能不能帮忙看一下qaq
CodeCode
CPP
//dot i used j times:
//(i,j) -> j*51+i
//q -> (q%51,q/51)
//50*51+50+3=2603

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,k,ans=INF,d[2603];
struct edge
{
    int to,cost;
    bool operator < (const edge &E) const
    { return cost<E.cost; }
};
vector<edge> G[2603];
priority_queue<edge> heap;

void add(int a,int b,int t)
{
    for(int i=0;i<=k;i++)
    {
        int u=a+i*51,v=b+i*51;
        G[u].push_back((edge){v,t});
    }
    for(int i=0;i<k;i++)
    {
        int u=a+i*51,v=b+(i+1)*51;
        G[u].push_back((edge){v,t/2});
    }
}

void dijkstra(int s)
{
    memset(d,0x3f,sizeof(d));
    d[s]=0; heap.push((edge){s,0});
    while(!heap.empty())
    {
        edge TOP=heap.top(); heap.pop();
        int v=TOP.to,cost=TOP.cost;
        if(d[v]<cost) continue;
        for(int i=0;i<G[v].size();i++)
        {
            int to=G[v][i].to,w=G[v][i].cost;
            if(d[to]>d[v]+w)
            {
                d[to]=d[v]+w;
                heap.push((edge){to,d[to]});
            }
        }
    }
}

int main()
{
    cin>>n>>m>>k;
    while(m--)
    {
        int a,b,t;
        cin>>a>>b>>t;
        add(a,b,t);
        add(b,a,t);
    }
    dijkstra(1);
    for(int i=0;i<=k;i++) ans=min(ans,d[i*51+n]);
    cout<<ans;
    return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...