1 条题解

  • 0
    @ 2025-8-24 21:41:35

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar XiaoX
    这个家伙真的很懒,什么也不想留下

    搬运于2025-08-24 21:41:35,当前版本为作者最后更新于2018-04-26 18:47:40,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    谁说不能用Dijkstra了?

    堆优化dijA了 (第2个点960ms。。。)

    上代码: (坑点挺多)

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<vector>
    using namespace std;
    typedef pair<double,int> kk;//注意小数*1
    const int N=5005,M=2000000;
    int n,m,s,t;
    int head[N],tot;
    double d[N];//注意小数*2
    bool v[N];
    struct edge{
    	int nxt,ver;
    	double w;//*3
    }e[M*2];
    void add(int x,int y,double z){
    	e[++tot].ver=y,e[tot].w=z,e[tot].nxt=head[x],head[x]=tot;
    }
    //邻接表存图(没啥说的吧)
    void dij(int st){
    	for(int i=1;i<=n;i++) d[i]=-1;//都赋值成-1(未到达)
    	priority_queue<kk>q;//大根堆
    	d[st]=1;q.push(make_pair(1,st));
    	while(q.size()){
    		int x=q.top().second;q.pop();
    		if(v[x]) continue;v[x]=1;
    		for(int i=head[x];i;i=e[i].nxt){
    			int y=e[i].ver;
    			if(d[x]*e[i].w>d[y]){
    				d[y]=d[x]*e[i].w;//注意题目要求(乘法)
    				q.push(make_pair(d[y],y));
    			}
    		}
    	}
    }
    int main ()
    {
    	scanf("%d%d%d%d",&n,&m,&s,&t);
    	for(int i=1;i<=m;i++){
    		int x,y;
    		double z;//注意小数*4...
    		scanf("%d%d%lf",&x,&y,&z);
    		add(x,y,z);//图存起来!!
    	}
    	dij(s);
    	if(d[t]!=-1) printf("%.4lf",d[t]);//-1说明不连通
    	else printf("orz");
    }
    
    • 1

    信息

    ID
    1837
    时间
    1000ms
    内存
    125MiB
    难度
    3
    标签
    递交数
    0
    已通过
    0
    上传者