我这里可以理解成无向图可以吗。
我觉得送出去再回来都是走那条最短的路
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=2*N;
const int inf=0x3f3f3f3f;
typedef pair<int,int> pii;
int h[M],d[M],idx;
struct node
{
int to,next,w;
}e[M],e2[M];
bool vis[N];
void add(int a,int b,int w)
{
e[idx].w=w;
e[idx].to=b;
e[idx].next=h[a];
h[a]=idx++;
}
void dj(int x)
{
priority_queue<pii,vector<pii>,greater<pii>>q;
q.push({0,x});
d[x]=0;
while(q.size()){
auto c=q.top();
int ver=c.second;
if(vis[ver])continue;
vis[ver]=1;
for(int i=h[ver];~i;i=e[i].next){
auto j=e[i];
if(d[j.to]>j.w+d[ver]){
d[j.to]=j.w+d[ver];
q.push({j.to,d[j.to]});
}
}
}
}
void solve()
{
int n,m;
cin>>n>>m;
memset(h,-1,sizeof h);
memset(d,inf,sizeof d);
memset(vis,0,sizeof vis);
while(m--){
int a,b,w;
cin>>a>>b>>w;
add(a,b,w);
add(b,a,w);
}
dj(1);
int ans=0;
for(int i=2;i<=n;i++){
ans+=d[i]*2;
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
solve();
return 0;
}