CF303DIV2 E 迪斯特拉加堆优化

//首先这道题是卡没有优化的迪斯特拉的。。。。然后就是数据的溢出问题。。。都开long long吧

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=300000+10;
const ll inf=99999999999999999;
int head[maxn];
int next[2*maxn];
int to[2*maxn];
ll val[2*maxn];
int viss[maxn];
int id[2*maxn];
int ans[maxn];
int tot;
typedef long long ll;
int inline read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct heapnode
{
    ll d;
    ll u;
    heapnode()
    {

    }
    heapnode(ll d1,ll u1):d(d1),u(u1)
    {
    }
    bool operator <(const heapnode &rhs)const
    {
        return d>rhs.d;
    }
};
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void inline addedge(int u,int v,ll value,int id1)
{
    to[tot]=v;
    val[tot]=value;
    id[tot]=id1;
    next[tot]=head[u];
    head[u]=tot++;
}
int visit[maxn];
ll low[maxn];
int n;
void inline dista(int start)
{
    memset(visit,0,sizeof(visit));
    ll sum=0;
    int k=0;
    low[start]=0;
    priority_queue<heapnode>que;
    heapnode x;
    x.d=0;
    x.u=start;
    que.push(x);
    while(!que.empty())
    {
        heapnode x=que.top();
        que.pop();
        if(visit[x.u])
            continue;
        visit[x.u]=1;
        for(int i=head[x.u];i!=-1;i=next[i])
        {
            int v=to[i];
            if(low[v]>low[x.u]+val[i])
            {
                low[v]=low[x.u]+val[i];
                que.push(heapnode(low[v],v));
            }
        }
    }

}
int main()
{
    int m;
    init();
    memset(viss,0,sizeof(viss));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        a=(ll)read();
        b=(ll)read();
        c=(ll)read();
        addedge(a,b,c,i);
        addedge(b,a,c,i);
    }
    for(int i=1;i<=n;i++)
        low[i]=inf;
    int start;
    scanf("%d",&start);
    dista(start);
    int k=0;
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        if(i!=start)
        {
            int pre=inf;
            int pos;
            for(int j=head[i];j!=-1;j=next[j])
            {
                int v=to[j];
               // cout<<i<<" vvvv"<<v<<endl;
              //  cout<<"low[i]  "<<low[i]<<" "<<low[v]<<" "<<val[j]<<endl;
                if(low[v]+val[j]==low[i]&&val[j]<pre/*&&viss[id[j]]==0*/)
                {

                    pre=val[j];
                    pos=id[j];
                }
            }
            sum+=pre;
            ans[k++]=pos;
            //viss[pos]=1;
        }
    }
    sort(ans,ans+k);
    printf("%lld\n",sum);
    for(int i=0;i<k;i++)
    {
        if(i!=k-1)
            printf("%d ",ans[i]);
        else
            printf("%d\n",ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值