其他分享
首页 > 其他分享> > Coloring Contention

Coloring Contention

作者:互联网

#include <bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#define N 10000000
#define M 10000000
using namespace std;
int js=0;
int h[N], e[M], w[M], ne[M], idx;
int dist[N], st[N];//表示该是否已经确定
//h头节点 e表边 w表示边权值 ne 表示下一条与起点连接的边的编号 idx 表示边的数量以及最新插入的边的编号
typedef pair<int, int> PII;
//记录节点的编号以及到节点的距离
void add(int a, int b)
{
    e[idx] = b,  ne[idx] = h[a], h[a] = idx++;
}
void dijkstra()  // 求1号点到n号点的最短路距离
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    //优先队列
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({1, 0});
    while (heap.size())
    {
        PII t = heap.top();
        heap.pop();
        int ver = t.first, distance = t.second;
        for (int i = h[ver]; i != -1; i = ne[i])
        {
            js++;
            int j = e[i];

            if (dist[j] > distance + 1)
            {
                dist[j] = distance + 1;
                heap.push({j,dist[j]});
            }
        }
    }
}
int main()
{
    int a,b;
    memset(h, -1, sizeof h);
    scanf("%d%d",&a,&b);
    for(int i=1; i<=b; i++)
    {
        int o,p;
        scanf("%d%d",&o,&p);
        add(o,p);
        add(p,o);
    }
    dijkstra() ;
    printf("%d",dist[a]-1);
}

标签:Coloring,dist,Contention,idx,int,ne,heap,include
来源: https://blog.csdn.net/weixin_46574282/article/details/119109288