zhouzl @ 2025-01-09 21:36:58
#include<bits/stdc++.h>
using namespace std;
struct bus{
int dian,money;
};
int n,m,cnt,dis1[1000001],dis2[1000001],vis1[1000001],vis2[1000001];
vector<bus> g1[1000001],g2[1000001];
bool operator<(bus a,bus b){
return a.money>b.money;
}
priority_queue<bus,vector<bus>,less<bus> > q1,q2;
void dijkstra1(int start1){
for (int i=1;i<=n;i++){
vis1[i]=0;
dis1[i]=1e9;
}
dis1[start1]=0;
q1.push({start1,0});
while(!q1.empty()){
bus temp1=q1.top();
q1.pop();
if(vis1[temp1.dian]==1) continue;
vis1[temp1.dian]=1;
for (int i=0;i<g1[temp1.dian].size();i++){
bus tem1=g1[temp1.dian][i];
if(dis1[tem1.dian]>dis1[temp1.dian]+tem1.money){
dis1[tem1.dian]=dis1[temp1.dian]+tem1.money;
q1.push({tem1.dian,dis1[tem1.dian]});
}
}
}
}
void dijkstra2(int start2){
for (int i=1;i<=n;i++){
vis2[i]=0;
dis2[i]=1e9;
}
dis2[start2]=0;
q2.push({start2,0});
while(!q2.empty()){
bus temp2=q2.top();
q2.pop();
if(vis2[temp2.dian]==1) continue;
vis2[temp2.dian]=1;
for (int i=0;i<g2[temp2.dian].size();i++){
bus tem2=g2[temp2.dian][i];
if(dis2[tem2.dian]>dis2[temp2.dian]+tem2.money){
dis2[tem2.dian]=dis2[temp2.dian]+tem2.money;
q2.push({tem2.dian,dis2[tem2.dian]});
}
}
}
}
int main()
{
cin>>n>>m;
for (int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
g1[u].push_back({v,w});
g2[v].push_back({u,w});
}
dijkstra1(1);
dijkstra2(1);
for (int i=1;i<=n;i++){
cnt+=dis1[i];
cnt+=dis2[i];
// cout<<dis1[i]<<" "<<dis2[i]<<endl;
}
cout<<cnt;
return 0;
}