Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4-InSange #15

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2024.03.11 | BFS | [์ˆจ๋ฐ”๊ผญ์งˆ 4](https://www.acmicpc.net/problem/13913) | [#1](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/3) |
| 2์ฐจ์‹œ | 2024.03.15 | DP | [์•ฑ](https://www.acmicpc.net/problem/7579) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/6) |
| 3์ฐจ์‹œ | 2024.03.19 | ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ | [์•ฑ](https://www.acmicpc.net/problem/7579) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/6) |
| 3์ฐจ์‹œ | 2024.03.19 | ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ | [์•ฑ](https://www.acmicpc.net/problem/7579) | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/9) |
| 4์ฐจ์‹œ | 2024.03.25 | ๋‹ค์ต์ŠคํŠธ๋ผ | [ํŒŒํ‹ฐ](https://www.acmicpc.net/problem/1238) | [#4]([https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/15)https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/15) |
=======
---
122 changes: 122 additions & 0 deletions InSange/๋‹ค์ต์ŠคํŠธ๋ผ/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAX_INT = 99999999;
int N, M, X, n, m, t, answer;
// vector<vector<int>> towns;
/// <summary>
/// ๏ฟฝรท๏ฟฝ๏ฟฝฬต๏ฟฝ ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ
/// </summary>
//void Floyd()
//{
//
// cin >> N >> M >> X;
//
// answer = 0;
// towns.assign(N+1, vector<int>(N+1, MAX_INT));
//
// while (M--)
// {
// cin >> n >> m >> t;
// towns[n][m] = t;
// }
//
// for (int k = 1; k <= N; k++)
// {
// towns[k][k] = 0;
// for (int i = 1; i <= N; i++)
// {
// if (towns[i][k] == MAX_INT) continue;
// for (int j = 1; j <= N; j++)
// {
// towns[i][j] = min(towns[i][j], towns[i][k] + towns[k][j]);
// }
// }
// }
//
// for (int i = 1; i <= N; i++)
// {
// answer = max(answer, towns[i][X] + towns[X][i]);
// }
//
// cout << answer << "\n";
//}

vector<vector<pair<int, int>>> towns;
vector<int> dist;
int result, temp;
/// <summary>
/// ๏ฟฝ๏ฟฝ๏ฟฝอฝ๏ฟฝฦฎ๏ฟฝ๏ฟฝ
/// </summary>
void dijkstra(int start, int end)
{
dist.clear();
dist.resize(N + 1, MAX_INT);
dist[start] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0, start });

while (!pq.empty())
{
int cur = pq.top().second;
int cost = pq.top().first;
pq.pop();

if (cur == end)
{
temp = dist[cur];
break;
}
for (auto road : towns[cur])
{
int next = road.first;
int next_cost = road.second + cost;

if (next_cost < dist[next])
{
dist[next] = next_cost;
pq.push({ next_cost, next });
}
}
}
}

void Solve()
{
cin >> N >> M >> X;

answer = 0;
towns.assign(N + 1, vector<pair<int ,int>>());

while (M--)
{
cin >> n >> m >> t;
towns[n].push_back({ m, t });
}

for (int i = 1; i <= N; i++)
{
result = 0;
dijkstra(i, X);
result += temp;
dijkstra(X, i);
result += temp;
answer = max(answer, result);
}

cout << answer;
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Solve();

return 0;
}