最短路球调
  • 板块学术版
  • 楼主TARGETMINE
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/17 19:51
  • 上次更新2023/11/2 13:13:38
查看原帖
最短路球调
935263
TARGETMINE楼主2023/10/17 19:51
#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N=10e5+10, M = 2 * 10e5 +10, INF = 0x3f3f3f3f;

int h[N], e[M], w[M], nex[M], idx = 0;

int dist[N];
bool vis[N];
int cnt[N];

int n, m;



void add(int a,int b, int c)
{
	e[idx] = b;
	w[idx] = c;
	nex[idx] = h[a];
	h[a] = idx++;
	
	
}


void dijkstra()
{
	for (int i=0;i<N;i++)
	{
		vis[i] = false;
		dist[i] = INF;
	}
	
	
	dist[1] = 0;

	
	priority_queue<PII, vector<PII>, greater<PII> > heap;
	
	heap.push({0, 1});
	while (heap.size())
	{
		int t = heap.top().second;
		heap.pop();
		
		if (vis[t]) continue;
		vis[t] = 1;
		
		for (int i=h[t]; i!=-1;i = nex[i])
		{
			int j = e[i];
			
			if (dist[j] > dist[t]+w[i])
			{

				cnt[j] = cnt[t];

				
				dist[j] = dist[t]+w[i];
				heap.push({dist[j], j});
			}
			if (dist[j] == dist[t] + w[i])
			{
				cnt[j] += cnt[t];
			}
			
			
		}
		
		
	}
	
	
}



int main()
{
	for (int i=0;i<N;i++)
	{
		h[i] = -1;
	}
	
	int a, b;
	cin >> n >> m;
	
	for (int i=0;i<m;i++)
	{
		cin >> a >> b;
		add(a, b, 1);
		add(b, a, 1);
		
	}
	dijkstra();
	
	for (int i=1;i<=n;i++)
	{
		cout << cnt[i] << endl;
	}
	
	return 0;
 } 

https://www.luogu.com.cn/problem/P1144

球调

2023/10/17 19:51
加载中...