附上代码
/************************************************
*Author : demonlover
*Created Time : 2022.05.21.20:42
*Problem : ABC252E
************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
#define int ll
typedef long double ld;
typedef pair <int,int> pii;
template <typename T>
inline bool read(T &x) {
int f = 1,c = getchar();x = 0;
while (!isdigit(c)) {if (c == 45)f = -1;c = getchar();}
while (isdigit(c))x = (x<<3)+(x<<1)+(c^48),c = getchar();
return x *= f,true;
}
template <typename T,typename... Args>
inline bool read(T &x,Args &...args) {
return read(x) && read(args...);
}
namespace run {
const int maxn = 2e5+10,maxm = 2e5+10;
struct Edge {int nxt,to,w,id;}edge[maxm<<1];
int begn[maxn],e;
inline void add(int x,int y,int z,int id) {
edge[++e] = (Edge){begn[x],y,z,id};begn[x] = e;
return;
}
int dis[maxn];
int fro[maxn];
bool vis[maxn];
priority_queue< pii > q;
inline void dijkstra(int s) {
memset(dis,0x7f,sizeof(dis));
dis[s] = 0,q.push(make_pair(0,s));
while (!q.empty()) {
int x = q.top().second;q.pop();
if (vis[x])continue;
vis[x] = true;
for (int i = begn[x];i;i = edge[i].nxt) {
int y = edge[i].to;
if (dis[y] > dis[x]+edge[i].w) {
dis[y] = dis[x]+edge[i].w;
fro[y] = edge[i].id;
if (!vis[y])q.push(make_pair(-dis[y],y));
}
}
}
return;
}
int n,m;
inline bool main() {
read(n,m);
for (int i = 1,x,y,z;i <= m;++i)read(x,y,z),add(x,y,z,i),add(y,x,z,i);
dijkstra(1);
for (int i = 2;i <= n;++i)cout<<fro[i]<<" ";
cerr<<1.0*clock()/CLOCKS_PER_SEC<<"\n";
return 0;
}
}
#undef int
#define demonlover
int main() {
#ifdef demonlover
freopen("ABC252E.in","r",stdin);
freopen("ABC252E.out","w",stdout);
#endif
return run :: main();
}
标签:int,read,edge,test,inline,include,dis
From: https://www.cnblogs.com/Lonely923/p/16733099.html