少刷水题
看题目知道是最短路,于是我就用了个spfa,也没加优化就A了。
是新手适合的板子题啊。
AC code:
#include<bits/stdc++.h>
using namespace std;
const int N = 2000010;
int n, m, start, en;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[start] = 0;
st[start] = true;
queue<int> q;
q.push(start);
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
return dist[en];
}
int main()
{
memset(h, -1, sizeof h);
cin >> n >> m >> start >> en;
for(int i = 0; i < m; i ++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int t = spfa();
cout << t << endl;
return 0;
}
艹
我忘记了最短路板子hh
这就是水题吧没错