FireFly620 @ 2024-02-28 16:14:01
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<3)+(x<<1)+c-'0';c=getchar();}return x*f;}
struct graph{
int k;
int v;
int w;
}edge1[2000001],edge2[2000001];
int head1[1000001],head2[1000001],vis[1000001],num,f[1000001],ans,n,m;
typedef pair<int,int> P;
void add1(int from,int to,int dis){
edge1[++num].k=head1[from];
edge1[num].v=to;
edge1[num].w=dis;
head1[from]=num;
}
void add2(int from,int to,int dis){
edge2[num].k=head2[from];
edge2[num].v=to;
edge2[num].w=dis;
head2[from]=num;
}
int dijkstra(graph g[],int h[]){//封装
int sum=0;
priority_queue<P>q;
memset(f,0x7f,sizeof f);
memset(vis,0,sizeof vis);
f[1]=0;
q.push(P(0,1));
while(!q.empty()){
P t=q.top();q.pop();
int j=t.second;
if(vis[j])continue;
vis[j]=1;
for(int i=h[j];i;i=g[i].k){
int l=g[i].v;
if(f[l]>f[j]+g[i].w){
f[l]=f[j]+g[i].w;
q.push(P(f[l],l));
}
}
}
for(int i=1;i<=n;i++)
sum+=f[i];
return sum;
}
signed main(){
n=read();m=read();
while(m--){
int a=read(),b=read(),c=read();
add1(a,b,c);add2(b,a,c);
}
ans=dijkstra(edge1,head1)+dijkstra(edge2,head2);
cout<<ans;
return 0;
}
by S_A_T_E_L_L_I_T_E @ 2024-04-20 22:06:00
我也是,开了long long也WA前3:
#include<bits/stdc++.h>
using namespace std;
struct edge{
int v,w;
bool operator < (edge b)const{
return w>b.w;
}
}now,t,et;
long long n,m,sum,x,y,z;
vector<edge> rmp[1000005];
vector<edge> rrmp[1000005];
int dis[1000005],vis[1000005];
void dij(int s,vector<edge> mp[]){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<edge>q;
dis[s]=0;
now={s,0};
q.push(now);
while(!q.empty()){
now=q.top();
q.pop();
if(vis[now.v]==1){
continue;
}
vis[now.v]=1;
int len=mp[now.v].size();
for(int i=0;i<len;i++){
t=mp[now.v][i];
if(dis[now.v]+t.w<dis[t.v]){
dis[t.v]=dis[now.v]+t.w;
et={t.v,dis[t.v]};
q.push(et);
}
}
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d %d",&x,&y,&z);
rmp[x].push_back({y,z});
rrmp[y].push_back({x,z});
}
dij(1,rmp);
for(int i=1;i<=n;i++){
sum+=dis[i];
}
dij(1,rrmp);
for(int i=1;i<=n;i++){
sum+=dis[i];
}
printf("%d",sum);
return 0;
}
by S_A_T_E_L_L_I_T_E @ 2024-04-20 22:11:26
@zhuzile123 艹,输出lld,写成d了,请无视我的评论
by MC_OIer @ 2024-06-30 22:57:31
metoo,悬关求助
by MC_OIer @ 2024-07-02 09:49:58
chao我板子打错了,不要管我