hdu 1535

#include "bits/stdc++.h"
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N = 1e6+5;
ll t,P,Q,cnt,u[N],v[N],w[N];
ll head[N],ans[N],vis[N];
struct node{
    ll to,next,w;
}edge[N],l[N];
struct cmp{
    bool operator()(pair<ll,ll>a,pair<ll,ll>b){
        return a.first>b.first;
    }
};
void init(){
    cnt=0;
    memset(head,-1,sizeof head);
    for(ll i=1 ;i<=P;i++){
        ans[i]=inf;
        vis[i]=0;
    }
}
void add_edge(ll u,ll v , ll w){
    edge[cnt].w=w;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void djstl(ll x){
    priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>q;
    ans[x]=0;
    q.push({0,x});
    while(!q.empty())
    {
        ll p = q.top().second;
        q.pop();
        vis[p]=1;
        for( ll i = head[p] ; i!= -1 ;i=edge[i].next)
        {
            if(vis[edge[i].to] == 0 && ans[edge[i].to] > ans[p] + edge[i].w){
                ans[edge[i].to] = ans[p] + edge[i].w;
                q.push({ans[edge[i].to],edge[i].to});
            }
        }
    }
}
int main()
{
    while (~scanf("%d",&t)){
        while (t--)
        {
            scanf("%d%d",&P,&Q);
            init();
            for(ll i=1 ;i<=Q ;i++)
            {
                scanf("%d%d%d",&u[i],&v[i],&w[i]);
                add_edge(u[i],v[i],w[i]);
            }
            ll sum = 0;
            djstl(1);
            for(int i=2 ;i<=P;i++){
                sum+=ans[i];
            }
            init();
            for(ll i=1 ;i<=Q ;i++)
            {
                add_edge(v[i],u[i],w[i]);
            }
            djstl(1);
            for(int i=2 ; i<=P;i++){
                sum+=ans[i];
            }
           printf("%d\n",sum);
        }
    }
    return 0;
}

//写的很无语 不会用memcpy

题目的意思就是 1到所有点的距离  ,所有点到1的距离,求最短路 ,代码写的是先跑1到所有点的距离,然后在原先跑存的时候在存逆着跑的图,跑完正再跑逆着,结束

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值