求助样例问题
查看原帖
求助样例问题
679581
MrPython小河狸贝瓦楼主2023/2/8 05:02

以下是本题的样例。

P5683_5.in

13 15
1 7
7 3
1 10
10 2
10 12
7 8
1 7
3 10
6 7
5 9
7 10
6 10
1 10
7 11
1 6
2 2 3 6

P5683_5.out

12

题目输入格式中提到:

数据保证没有重边和自环。

可在本样例中,出现了两条1 10
本题我是用迪杰斯特拉做的,90pts,5,20 WA

#include <bits/stdc++.h>
using namespace std;
using ui = unsigned int;
using Graph = vector<vector<ui>>;
void dijkstra(Graph &mp, vector<pair<ui, ui>> &dis, ui s)
{
    using node = pair<ui, ui>;
    vector<bool> vis(mp.size());
    priority_queue<node, vector<node>, greater<node>> q;
    q.push(node(0, s));
    dis[s].first = 0;
    while (!q.empty())
    {
        ui tmp = q.top().second;
        q.pop();
        if (!vis[tmp])
        {
            vis[tmp] = true;
            for (vector<ui>::reference v : mp[tmp])
                if (dis[v].first > dis[tmp].first + 1)
                    dis[v].first = dis[tmp].first + 1,
                    dis[v].second = tmp,
                    q.push(node(dis[v].first, v));
        }
    }
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    ui n, m;
    cin >> n >> m;
    Graph mp(n);
    for (ui i = 0; i < m; i++)
    {
        ui x, y;
        cin >> x >> y;
        x--, y--;
        mp[x].push_back(y), mp[y].push_back(x);
    }
    vector<pair<ui, ui>> dis(n, {0x7fffffff, -1});
    dijkstra(mp, dis, 0);
    ui s1, t1, s2, t2;
    cin >> s1 >> t1 >> s2 >> t2;
    --s1, --s2;
    if (dis[s1].first > t1 || dis[s2].first > t2)
    {
        cout << "-1";
        return 0;
    }
    set<pair<ui, ui>> edges;
    for (ui i = s1, j = dis[i].second; ~j; i = j, j = dis[i].second)
        edges.insert({min(i, j), max(i, j)});
    for (ui i = s2, j = dis[i].second; ~j; i = j, j = dis[i].second)
        edges.insert({min(i, j), max(i, j)});
    cout << m - edges.size();
    return 0;
}
2023/2/8 05:02
加载中...