-
B - Por Costel and the Algorithm
- Gym - 100923B
-
#include<stdio.h> #include<cstring> #include<queue> #include<cstdio> #include<vector> #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn 1000005 #define inf 0x7fffffff ll head[maxn],cnt,pre[maxn]; ll u,v,w,sum,m,n,t; bool vis[maxn],out[maxn]; struct node { ll v,w,to; } edge[maxn]; struct hed { ll order,w; bool operator<(const hed &x)const { return w>x.w; } } top,dis[maxn]; void ad(ll u,ll v,ll w) { edge[cnt].v=v; edge[cnt].to=head[u]; edge[cnt].w=w; head[u]=cnt; cnt++; } void dijst() { dis[1].w=0; priority_queue<hed>q; q.push(dis[1]); while(!q.empty()) { top=q.top(); q.pop(); if(!vis[top.order]) { vis[top.order]=1; for(int i=head[top.order]; i!=-1; i=edge[i].to) { if(dis[edge[i].v].w>dis[top.order].w+edge[i].w) { dis[edge[i].v].w=dis[top.order].w+edge[i].w; q.push(dis[edge[i].v]); out[i]=1; out[pre[edge[i].v]]=0; pre[edge[i].v]=i; } } } } } void dfs(ll u) { for(ll i=head[u]; i!=-1; i=edge[i].to) { if(out[i]) { sum++; if(sum<m) printf("%lld ",i); else printf("%lld\n",i); dfs(edge[i].v); } } } int main() { freopen("algoritm.in","r",stdin); freopen("algoritm.out","w",stdout); scanf("%lld",&t); while(t--) { cnt=1; sum=0; scanf("%lld%lld",&n,&m); for(int i=1; i<=n; i++) { vis[i]=0; dis[i].order=i; dis[i].w=inf; head[i]=-1; pre[i]=0; } for(int i=1; i<=m; i++) { out[i]=0; scanf("%lld%lld%lld",&u,&v,&w); ad(u,v,w); } dijst(); dfs(1); for(ll i=1; i<=m; i++) if(!out[i]) { sum++; if(sum<m) printf("%lld ",i); else printf("%lld\n",i); } } return 0; }
B - Por Costel and the Algorithm-最短路记录边
最新推荐文章于 2020-07-28 20:43:58 发布