寒假:Day22

Day22

340. 通信线路 - AcWing题库

#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;
}

1137. 选择最佳线路 - AcWing题库

遇到多起点单终点的情况,可以把每一条边反着建,然后把终点看成起点,起点看成终点,反着跑一遍最短路,然后找到距离最短的即可。但是如果遇到了多起点多终点的,这个时候就需要建立一个虚拟源点,然后往每一个起点连一条边权为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;
}

1134. 最短路计数 - AcWing题库

计算最短路的条数,只需要注意两点转换即可

  1. 如果当前距离大于记录的最小距离,更新距离: 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]
  2. 如果当前距离等于记录的最小距离,更新距离: 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值