1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ワーシャルフロイド法

Last updated at Posted at 2022-02-26

最短経路問題

問題 : 動作確認済み

(条件)
頂点数 n のグラフ
頂点a, b 間の距離を求める

int c[10][10];
int H, W, A, res;

int main()
{
	cin >> H >> W; //縦、横のマス
	rep(i, 0, 10) rep(j, 0, 10) cin >> c[i][j]; // i から j の変換に必要な魔力


	//ワーシャルフロイド法
	rep(k, 0, 10) rep(i, 0, 10) rep(j, 0, 10)
		if (c[i][j] > c[i][k] + c[k][j]) c[i][j] = c[i][k] + c[k][j];


	rep(i, 0, H) rep(j, 0, W)
	{
		cin >> A;
		if (A >= 0) res += c[A][1]; // 数字A から 1 に変える魔力の和
	}

	cout << res << endl;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?