题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1010, M = 20010;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];
int n, m, k;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool check(int bound)
{
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
deque<int> dq;
dq.push_back(1);
dist[1] = 0;
while(dq.size())
{
auto t = dq.front();
dq.pop_front();
if (t == n) break;
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i], c = w[i] > bound;
if (dist[j] > dist[t] + c)
{
dist[j] = dist[t] + c;
if (c) dq.push_back(j);
else dq.push_front(j);
}
}
}
return dist[n] <= k;
}
int main()
{
cin >> n >> m >> k;
memset(h, -1, sizeof h);
while(m --)
{
int a, b, c;
cin >> a >> b >>c;
add(a, b, c);
add(b, a, c);
}
int l = 0, r = 1e6 + 1;
while(l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (r == 1e6 + 1) cout << -1;
else cout << r;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
这里也很迷
为什么要用双端队列,还有这一步什么意思哇,