C-Coloring Contention

Coloring Contention

This question roughly means that Alice and Bob are playing a game. In a graph, there are n points and m edges. Alice wants Bob to take as many edges with different colors as possible, so she puts the adjacent edges They are all painted in different colors. And Bob has to walk the side with the least different colors, and ask Bob how many colors need to be changed at least when taking this picture.
If you think about it carefully, it is the shortest path-1. Because which way to go is to change the color from one path to another, Bob takes the shortest path~
Why do you want to make up this question, because there is no time to read it during the game. I've been looking at the evil question E. As for what question E is: Question E Portal
One is that I didn't play during the game, and the other is I want to review the shortest path.

So the code is as follows:
The method used is dijkstra + heap optimization.

#include <bits/stdc++.h>
using namespace std;

const int N = 200010;

int dist[N];
int e[N], ne[N], w[N], h[N];
bool st[N];
int idx;
int n, m;
typedef pair<int, int> PII;
void add(int a, int b)
{
    
    
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

int dijkstra()
{
    
    
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({
    
    1, 0});
    while(heap.size())
    {
    
    
        auto t = heap.top();
        heap.pop();
        int ver = t.first, distance = t.second;
        for(int i = h[ver]; i != -1; i = ne[i])
        {
    
    
            int j = e[i];
            if(dist[j] > distance + 1)
            {
    
    
                dist[j] = distance + 1;
                heap.push({
    
    j, dist[j]});
            }
        }
    }
    return dist[n];
}



int main()
{
    
       
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for(int i = 0; i < m; i ++)
    {
    
    
        int a, b;
        cin >> a >> b;
        add(a, b);
        add(b, a);
    }
    int t = dijkstra();
    cout << t - 1 << endl;
}



Guess you like

Origin blog.csdn.net/qq_47783181/article/details/113101045