dijkstra+小跟堆90pts, WA#1 求调!
查看原帖
dijkstra+小跟堆90pts, WA#1 求调!
1086872
ImSakura楼主2024/8/1 22:33

人已麻,本来打算把⬇️

//......
int Min = 1e9;
int u =0;
//找出Cost Min
for(int t=1;t<=n;t++){
	if(!vis[t]&&cost[t]<Min)
      Min=cost[t],u=t;
}	
//......

用优先队列代替的 没想到#1 WA了 其他都AC 求调

#include <bits/stdc++.h>
using namespace std;
#define N 1001
#define inf 1e6
int cost[N];//from 1 -> n
int out[N][N];//俩药水合成
int n, m;
int vis[N];//是否更新过其cost 
int ans[N];//ans[i] i的最便宜的方案数
void dijkstra() {
	priority_queue<pair<int, int>> q;
	for (int i = 1; i <= n; i++) {
		q.push({-cost[i], i});
	}
	int u;
	while (q.size()) {
		u=q.top().second;
		q.pop();
		for (int v = 1; v <= n; v++) {
		vis[u]=1;
			if (vis[v] && out[u][v] != 0) {
				int o = out[u][v];
				if (cost[o] == cost[u] + cost[v])ans[o] += ans[u] * ans[v];
				else if (cost[o] > cost[u] + cost[v])cost[o] = cost[u] + cost[v], ans[o] = ans[u] * ans[v];
			}
		}
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> cost[i];
		ans[i] = 1; //从商店买
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			out[i][j] = out[j][i] = 0;
		}
	}
	//下标从1开始计算
	int a, b, c;
	while (cin >> a >> b >> c) {
		a++, b++;
		c++;
		out[a][b] = out[b][a] = c;
	}
	dijkstra();
	cout << cost[1] << " " << ans[1];
	return 0;
}
2024/8/1 22:33
加载中...