1. 程式人生 > >LUOGU P1342 請柬(最短路)

LUOGU P1342 請柬(最短路)

digi per con clu val https ide tps i++

傳送門

解題思路

又是一道語文題,弄清楚題意之後其實就能想出來了,從1跑一遍最短路,把$dis[n]$加入答案。在建個反圖跑一遍最短路,把$dis[n]_$加入最短路就行了。第一遍是去的時候,第二遍是回的時候。

技術分享圖片
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;
const int MAXN = 1000005;
typedef long long LL;

inline int
rd(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)) {f=ch==-?0:1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-0;ch=getchar();} return f?x:-x; } int n,m,head[MAXN],cnt; int to[MAXN],nxt[MAXN],val[MAXN]; int head_[MAXN],cnt_,val_[MAXN],nxt_[MAXN],to_[MAXN]; LL dis[MAXN],ans;
bool vis[MAXN]; struct Node{ int w,id; friend bool operator<(const Node A,const Node B){ return A.w>B.w; } }; priority_queue<Node> Q; inline void add(int bg,int ed,int w){ to[++cnt]=ed,val[cnt]=w,nxt[cnt]=head[bg],head[bg]=cnt; } inline void add_(int bg,int ed,int
w){ to_[++cnt_]=ed,val_[cnt_]=w,nxt_[cnt_]=head_[bg],head_[bg]=cnt_; } void dijkstra(){ memset(dis,0x3f,sizeof(dis)); Node now;now.id=1;now.w=0;dis[1]=0;Q.push(now); while(!Q.empty()){ Node zz=Q.top();Q.pop();int x=zz.id; if(dis[x]!=zz.w || vis[x]) continue;vis[x]=1; for(register int i=head[x];i;i=nxt[i]){ int u=to[i]; if(dis[u]<=dis[x]+val[i]) continue; dis[u]=dis[x]+val[i]; now.id=u;now.w=dis[u];Q.push(now); } } for(register int i=2;i<=n;i++) ans+=dis[i]; } void dijkstra_(){ memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); Node now;now.id=1;now.w=0;dis[1]=0;Q.push(now); while(!Q.empty()){ Node zz=Q.top();Q.pop();int x=zz.id; if(dis[x]!=zz.w || vis[x]) continue;vis[x]=1; for(register int i=head_[x];i;i=nxt_[i]){ int u=to_[i]; if(dis[u]<=dis[x]+val_[i]) continue; dis[u]=dis[x]+val_[i]; now.id=u;now.w=dis[u];Q.push(now); } } for(register int i=2;i<=n;i++) ans+=dis[i]; } int main(){ n=rd(),m=rd();int x,y,z; for(register int i=1;i<=m;i++){ x=rd(),y=rd(),z=rd(); add(x,y,z),add_(y,x,z); } dijkstra(),dijkstra_();cout<<ans; return 0; }
View Code

LUOGU P1342 請柬(最短路)