求条 Wa 25pts
  • 板块P1342 请柬
  • 楼主Lacuna
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/4/17 21:49
  • 上次更新2025/7/22 09:31:43
查看原帖
求条 Wa 25pts
699876
Lacuna楼主2025/4/17 21:49
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;

//#define LOCAL
#define gc getchar
#define pc putchar
#define LL long long
#define int long long
#define L(i,x,y) for(register int i=x;i<=y;i++)
#define R(i,x,y) for(register int i=x;i>=y;i--)

const int N=1e6+5;

struct edge{int v; LL w;};
vector<edge> G[N],e[N];
int n,m; LL ans,dis[N];
bool vis[N];

inline LL read(){LL x=0,f=1; char ch=gc(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=gc();} while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=gc();} return x*f;}
inline void write_(LL x){if(x<0) x=-x,pc('-'); if(x>9) write_(x/10); pc(x%10+'0');}
inline void write(LL x){write_(x); pc('\n');}

void Dijkstra(int s){
  memset(vis,0,sizeof vis);
  L(i,0,N-1) dis[i]=2147483647;
  priority_queue<pair<LL,LL> > q;
  dis[s]=0; q.push(make_pair(s,0));
  while(!q.empty()){
    pair<int,LL> t=q.top(); q.pop();
    int u=t.first; if(vis[u]) continue;
    vis[u]=1;
    for(auto e:G[u]){
      int v=e.v; LL w=e.w;
      if(dis[v]>dis[u]+w) dis[v]=dis[u]+w,q.push({v,dis[v]});
	}
  }
}
void dijkstra(int s){
  memset(vis,0,sizeof vis);
  L(i,0,N-1) dis[i]=2147483647;
  priority_queue<pair<LL,LL> > q;
  dis[s]=0; q.push(make_pair(s,0));
  while(!q.empty()){
    pair<int,LL> t=q.top(); q.pop();
    int u=t.first; if(vis[u]) continue;
    vis[u]=1;
    for(auto g:e[u]){
      int v=g.v; LL w=g.w;
      if(dis[v]>dis[u]+w) dis[v]=dis[u]+w,q.push({v,dis[v]});
	}
  }
}

signed main(){
  #ifdef LOCAL
    freopen("P1342_1.in","r",stdin);
    freopen("ans.out","w",stdout);
  #endif
  n=read(),m=read();
  L(i,1,m){
  	LL u=read(),v=read(),w=read();
  	G[u].push_back({v,w}); e[v].push_back({u,w});
  }
  Dijkstra(1);
  L(i,2,n) ans+=dis[i];
  dijkstra(1);
  L(i,2,n) ans+=dis[i];
  write(ans);
  return 0;
}
2025/4/17 21:49
加载中...