Day22
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 20010;
int n, m, k;
int h[N], e[M], ne[M], w[M], idx;
deque<int> q;
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++;
}
bool check(int bound)
{
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
q.push_back(1);
while (q.size())
{
int t = q.front();
q.pop_front();
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i], v = w[i] > bound;
if (dist[j] > dist[t] + v)
{
dist[j] = dist[t] + v;
if (v) q.push_back(j);
else q.push_front(j);
}
}
}
return dist[n] <= k;
}
int main(void)
{
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) r = -1 ;
cout << r << endl ;
return 0;
}
遇到多起点单终点的情况,可以把每一条边反着建,然后把终点看成起点,起点看成终点,反着跑一遍最短路,然后找到距离最短的即可。但是如果遇到了多起点多终点的,这个时候就需要建立一个虚拟源点,然后往每一个起点连一条边权为0的边,这样就变成了单源最短路了,再跑一遍模板找到最近的终点即可,思路很巧妙。
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 21010;
int n, m, T;
int h[N], e[M], ne[M], w[M], idx;
int dist[N], q[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int spfa()
{
int hh = 0, tt = 0;
memset(dist, 0x3f, sizeof dist);
dist[0] = 0;
q[tt ++ ] = 0;
st[0] = true;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
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])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
if (dist[T] == 0x3f3f3f3f) return -1;
return dist[T];
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n >> m >> T) {
memset(h, -1, sizeof h);
idx = 0;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int s;
cin >> s;
while (s--) {
int ver;
cin >> ver;
add(0, ver, 0);
}
cout << spfa() << endl ;
}
return 0;
}
计算最短路的条数,只需要注意两点转换即可
- 如果当前距离大于记录的最小距离,更新距离: d i s t [ j ] = d i s t [ t ] + 1 dist[j] = dist[t] + 1 dist[j]=dist[t]+1 ,更新条数: c n t [ j ] = c n t [ t ] cnt[j] = cnt[t] cnt[j]=cnt[t]
- 如果当前距离等于记录的最小距离,更新距离: d i s t [ j ] + = d i s t [ t ] dist[j] += dist[t] dist[j]+=dist[t]
#include<bits/stdc++.h>
using namespace std;
const int N = 100010, M = 400010, mod = 100003;
int h[N], e[M], ne[M], idx;
int cnt[N], dist[N];
int n, m;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void bfs()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
cnt[1] = 1;
queue<int> q;
q.push(1);
while (q.size())
{
int t = q.front();
q.pop();
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + 1)
{
dist[j] = dist[t] + 1;
cnt[j] = cnt[t];
q.push(j);
}
else if (dist[j] == dist[t] + 1){
cnt[j] = (cnt[j] + cnt[t]) % mod;
}
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
while (m--)
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
bfs();
for (int i = 1; i <= n; i++) cout << cnt[i] << endl ;
return 0;
}