求助,92分第9个点WA了,不知道为什么qwq
查看原帖
求助,92分第9个点WA了,不知道为什么qwq
1037516
DreamTsinghua楼主2023/10/18 12:26
#include <bits/stdc++.h>

using namespace std ;

typedef pair <int, int> PII ;
priority_queue<PII, vector<PII>, greater<PII>>heap ;
int n, m ;

const int N = 4e6 + 10  ;
int e[N], ne[N], w[N], h[N] ;
int dist[N], cnt[N] ;
int idx ;
bool st[N] ;
const int M = 2e3 + 10 ;
bool vis[M][M] ;

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

void Dijkstra() {
	memset(dist, 0x3f, sizeof dist) ;
	cnt[1] = 1 ;
	dist[1] = 0 ;
	heap.push({0, 1}) ;

	while (heap.size()) {
		auto t = heap.top() ;
		heap.pop() ;

		int ver = t.second ;
		if (st[ver])
			continue ;
		st[ver] = 1 ;

		for (int i = h[ver] ; i != -1 ; i = ne[i]) {
			int j = e[i] ;
			if (dist[j] > dist[ver] + w[i]) {
				dist[j] = dist[ver] + w[i] ;
				cnt[j] = cnt[ver] ;
				heap.push({dist[j], j}) ;
			} else if (dist[j] == dist[ver] + w[i])
				cnt[j] += cnt[ver] ;
		}
	}
}

int main() {
	cin >> n >> m ;

	memset(h, -1, sizeof h) ;
	for (int i = 1; i <= m ; i ++) {
		int a, b, c;
		cin >> a >> b >> c ;
		if (!vis[a][b])
			add(a, b, c), vis[a][b] = 1  ;
	}

	Dijkstra() ;
	if (dist[n] == 0x3f3f3f3f)
		cout << "No answer" << endl ;
	else
		cout << dist[n] << ' ' << cnt[n] ;
	return 0 ;

}
2023/10/18 12:26
加载中...