沧澜 @ 2017-08-22 20:55:25
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
using namespace std;
int n,m,tot,tot1;
int vis[MAXN];
int to[MAXN],net[MAXN],head[MAXN];
int to1[MAXN],net1[MAXN],head1[MAXN];
long long dis[MAXN],cap1[MAXN],cap[MAXN],ans;
void add(int u,int v,long long w){
to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
to1[++tot1]=u;net1[tot1]=head1[v];cap1[tot1]=w;head1[v]=tot1;
}
void spfa(int s){
queue<int>que;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
que.push(s);
vis[s]=1;dis[s]=0;
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=0;
for(int i=head[now];i;i=net[i]){
if(dis[to[i]]>dis[now]+cap[i]){
dis[to[i]]=dis[now]+cap[i];
if(!vis[to[i]]){
vis[to[i]]=1;
que.push(to[i]);
}
}
}
}
}
void spfa1(int s){
queue<int>que;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
que.push(s);
vis[s]=1;dis[s]=0;
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=0;
for(int i=head1[now];i;i=net1[i]){
if(dis[to1[i]]>dis[now]+cap1[i]){
dis[to1[i]]=dis[now]+cap1[i];
if(!vis[to1[i]]){
vis[to1[i]]=1;
que.push(to1[i]);
}
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
spfa(1);
for(int i=1;i<=n;i++) ans+=dis[i];
spfa1(1);
for(int i=1;i<=n;i++) ans+=dis[i];
printf("%lld",ans);
}
by 沧澜 @ 2017-08-22 21:10:43
谢谢各位,原因已找到,多定义了3个long long数组,所以TLE了。
by 沧澜 @ 2017-08-22 21:16:15
但蒟蒻的我还是不明白为什么会TLE,┭┮﹏┭┮
by 103PA @ 2020-06-25 11:33:17
IEE