最短路板子题。。
写个spfa。。。嘿嘿嘿‘’
以下是AC代码
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
#define pb push_back
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
int tot,head[maxn];
struct node
{
int t, v, nxt;
}ed[maxn];
void add(int a,int b,int v)
{
ed[++tot].t = b;
ed[tot].v = v;
ed[tot].nxt = head[a];
head[a] = tot;
}
bool vis[maxn];
int dis[maxn];
int t,c,ts,te;
void spfa()
{
queue<int>q;
vis[ts] = true;
dis[ts] = 0;
q.push(ts);
while(q.size())
{
int u=q.front();q.pop();
vis[u] = false;
for(int i=head[u];~i;i=ed[i].nxt)
{
int t = ed[i].t;
int v = ed[i].v;
if(dis[t] >= dis[u]+v)
{
dis[t] = dis[u] + v;
if(!vis[t])
{
q.push(t);
vis[t] = true;
}
}
}
}
}
int main()
{
t = read(); c = read();
ts = read(); te =read();
memset(vis, false, sizeof vis);
memset(dis, INF, sizeof dis);
memset(head, -1, sizeof head);
tot = 0;
for(int i=1;i<=c;i++)
{
int a=read(), b=read(), c=read();
add(a,b,c);
add(b,a,c);
}
spfa();
printf("%d\n",dis[te]);
return 0;
}