Moving Both Hands
| |
| |
| |
| |
| |
| |
| |
| #include <bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| const int inf=1e18; |
| const int M=2e5+5; |
| using pii=pair<int,int>; |
| |
| |
| int h1[M],ne1[M],e1[M],w1[M],tot1; |
| int h2[M],ne2[M],e2[M],w2[M],tot2; |
| |
| void add1(int from,int to,int wi) { |
| e1[++tot1]=to; |
| w1[tot1]=wi; |
| ne1[tot1]=h1[from]; |
| h1[from]=tot1; |
| } |
| |
| void add2(int from,int to,int wi) { |
| e2[++tot2]=to; |
| w2[tot2]=wi; |
| ne2[tot2]=h2[from]; |
| h2[from]=tot2; |
| } |
| |
| int n,m; |
| int dis[M][2]; |
| bool vis[M][2]; |
| |
| void dij() { |
| for(int i=1;i<=n;i++) |
| dis[i][0]=dis[i][1]=inf; |
| dis[1][0]=dis[1][1]=0; |
| priority_queue<pii,vector<pii>,greater<pii>>q; |
| q.push({dis[1][0],1}); |
| q.push({dis[1][1],-1}); |
| while(!q.empty()) { |
| auto [st,now]=q.top(); |
| q.pop(); |
| if(now>0&&vis[now][0]==0) { |
| vis[now][0]=1; |
| for(int i=h1[now];i;i=ne1[i]) { |
| int to=e1[i],wi=w1[i]; |
| if(dis[to][0]>st+wi) { |
| dis[to][0]=st+wi; |
| q.push({dis[to][0],to}); |
| } |
| } |
| for(int i=h2[now];i;i=ne2[i]) { |
| int to=e2[i],wi=w2[i]; |
| if(dis[to][1]>st+wi) { |
| dis[to][1]=st+wi; |
| q.push({dis[to][1],-to}); |
| } |
| } |
| } |
| if(now<0&&vis[-now][1]==0) { |
| now=abs(now); |
| vis[now][1]=1; |
| for(int i=h2[now];i;i=ne2[i]) { |
| int to=e2[i],wi=w2[i]; |
| if(dis[to][1]>st+wi) { |
| dis[to][1]=st+wi; |
| q.push({dis[to][1],-to}); |
| } |
| } |
| } |
| } |
| } |
| |
| |
| signed main() { |
| cin>>n>>m; |
| while(m--) { |
| int u,v,wi; |
| cin>>u>>v>>wi; |
| add1(u,v,wi); |
| add2(v,u,wi); |
| } |
| dij(); |
| for(int i=2;i<=n;i++) { |
| int ans=min(dis[i][0],dis[i][1]); |
| if(ans==inf)cout<<-1<<' '; |
| else cout<<ans<<' '; |
| } |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!