社区讨论

dijkstra+小跟堆90pts, WA#1 求调!

P1875佳佳的魔法药水【数据有误】参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lzbdnb5v
此快照首次捕获于
2024/08/01 22:34
2 年前
此快照最后确认于
2024/08/02 09:09
2 年前
查看原帖
人已麻,本来打算把⬇️
C
//......
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 求调
C
#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;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...