社区讨论

家人们我有个困惑

题目总版参与者 4已保存回复 6

讨论操作

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

当前回复
6 条
当前快照
1 份
快照标识符
@m5xcs76o
此快照首次捕获于
2025/01/15 11:39
去年
此快照最后确认于
2025/01/15 14:26
去年
查看原帖
P1119 为啥这个是对的
CPP
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
int t[N], f[N][N], n, m, q, tot;
signed main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)cin >> t[i];
	memset(f, 0x3f, sizeof(f));
	for (int i = 0; i < n; i++)f[i][i] = 0;
	for (int i = 1; i <= m; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		f[x][y] = f[y][x] = z;
	}
	cin >> q;
	while(q--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		while(t[tot] <= z && tot < n)
		{
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					f[i][j] = f[j][i] = min(f[i][j], f[i][tot] + f[tot][j]);
			tot++;
		}
		if (t[x] > z || t[y] > z)cout << -1 << endl;
		else
		{
			if (f[x][y] >= 1e9)cout << -1 << endl;
			else cout << f[x][y] << endl;
		}
	}
	return 0;
}
但这个有问题
CPP
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
int t[N], f[N][N], n, m, q, tot;
signed main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> t[i];
	memset(f, 0x3f, sizeof(f));
	for (int i = 1; i <= n; i++)f[i][i] = 0;
	for (int i = 1; i <= m; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		f[x][y] = f[y][x] = z;
	}
	cin >> q;
	while(q--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		while(t[tot] <= z && tot < n)
		{
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					f[i][j] = f[j][i] = min(f[i][j], f[i][tot] + f[tot][j]);
			tot++;
		}
		if (t[x] > z || t[y] > z)cout << -1 << endl;
		else
		{
			if (f[x][y] >= 1e9)cout << -1 << endl;
			else cout << f[x][y] << endl;
		}
	}
	return 0;
}

回复

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

正在加载回复...