家人们我有个困惑
  • 板块题目总版
  • 楼主JackyNo1
  • 当前回复6
  • 已保存回复6
  • 发布时间2025/1/15 11:38
  • 上次更新2025/1/15 14:26:11
查看原帖
家人们我有个困惑
773288
JackyNo1楼主2025/1/15 11:38

P1119 为啥这个是对的

#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;
}

但这个有问题

#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;
}
2025/1/15 11:38
加载中...