Skip to content

Commit

Permalink
Merge pull request #15 from AlgoLeadMe/4-InSange
Browse files Browse the repository at this point in the history
4-InSange
  • Loading branch information
InSange authored Mar 26, 2024
2 parents 3582945 + 35a00df commit 034adf7
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
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;
}

0 comments on commit 034adf7

Please sign in to comment.