Demon_master @ 2021-10-10 23:14:28
#include<bits/stdc++.h>
using namespace std;
const int maxn=(10+1e6)*2,INTMAX=0x3f3f3f3f,maxm=(1e6+10)*2;
int n,m;
struct E{
int to,next,val;
}edge[maxn];
int head_1[maxm],head_2[maxm];
struct T{
long long num,val;
T(){}
T(long long a,long long b){num=a,val=b;}
};
bool operator <(const T a,const T b){
return a.val>b.val;
}
int len=1;
void read(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
if(from==to) continue;
edge[len].to=to;
edge[len].val=val;
edge[len].next=head_1[from];
head_1[from]=len;
len++;
edge[len].to=from;
edge[len].val=val;
edge[len].next=head_2[to];
head_2[to]=len;
len++;
}
}
bool vis[maxm];
long long dis_1[maxm];
long long dis_2[maxm];
void work(int s){
memset(dis_1,0x3f,sizeof dis_1);
priority_queue<T> q;
dis_1[s]=0;
q.push(T(s,0));
while(!q.empty()){
T p=q.top();q.pop();
if(vis[p.num]) continue;
vis[p.num]=1;
long long num=p.num,diss=p.val;
for(int i=head_1[num];i;i=edge[i].next){
long long num_=edge[i].to,diss_=edge[i].val;
if(vis[num_]) continue;
if(dis_1[num_]>diss+diss_){
q.push(T(num_,diss+diss_));
dis_1[num_]=diss+diss_;
}
}
}
memset(dis_2,0x3f,sizeof dis_2);
memset(vis,0,sizeof vis);
// priority_queue<T> q;
dis_2[s]=0;
q.push(T(s,0));
while(!q.empty()){
T p=q.top();q.pop();
if(vis[p.num]) continue;
vis[p.num]=1;
long long num=p.num,diss=p.val;
for(int i=head_2[num];i;i=edge[i].next){
long long num_=edge[i].to,diss_=edge[i].val;
if(vis[num_]) continue;
if(dis_2[num_]>diss+diss_){
q.push(T(num_,diss+diss_));
dis_2[num_]=diss+diss_;
}
}
}
}
void P(){
long long ans=0;
for(int i=1;i<=n;i++){
ans+=dis_1[i];
ans+=dis_2[i];
// cout<<dis_1[i]<<" "<<dis_2[i]<<endl;
}
printf("%lld\n",ans);
}
int main (){
freopen("P1342_1 (1).in","r",stdin);
read();
work(1);
P();
}
by Nickle @ 2021-10-11 06:45:59
有没有可能是你忘记删freopen
或者这个题目严格扣换行符或者空格
by Nickle @ 2021-10-11 06:49:22
嗯,我去测了测,是freopen
的锅
by Demon_master @ 2021-10-13 16:33:29
@Nickle 谢谢