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

13 최단 경로 #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions 13/1238.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
typedef pair<int, int> ci;
const int INF = 2e5; //최대 경로 값 (간선의 수 * 가중치 최대값)

vector<int> dijkstra(int start, int v, vector<vector<ci>>& graph) {
vector<int> dist(v + 1, INF); // 각 정점까지의 최단 경로 저장
priority_queue<ci> pq;
// 시작 정점 초기화
dist[start] = 0;
pq.push({ 0, start });
while (!pq.empty()) {
int w = -pq.top().first; // 현재 정점까지의 경로값
int n = pq.top().second; // 현재 탐색하려는 정점
pq.pop();
if (w > dist[n]) { // 이미 다 작은 경로가 있다면 continue
continue;
}
for (auto [nxt_node, nxt_weight] : graph[n]) {
if (dist[nxt_node] > dist[n] + nxt_weight) { //최소값 갱신
dist[nxt_node] = dist[n] + nxt_weight;
pq.push({ -dist[nxt_node],nxt_node });
}
}
}
return dist;
}

int main() {
int v, e, k, a, b, w;
int answer = 0;
//입력
cin >> v >> e >> k;
vector<vector<ci>> graph(v + 1, vector<ci>(0)); // 인접 리스트
while (e--) {
cin >> a >> b >> w;
graph[a].push_back({ b, w });
}
// 연산
// 가고 오는 시간을 다익스트라 알고리즘을 사용해 따로 구한다.
vector<int> go(v + 1), back;
//파티 장소로 가는데 걸리는 시간 (모든 정점 -> 파티 장소)
for (int i = 1; i <= v; i++) {
vector<int> tmp = dijkstra(i, v, graph);
go[i] = tmp[k]; //각 정점에서 출발했을 때 파티 장소까지의 최단 경로
}
// 집으로 오는데 걸리는 시간 (파티 장소 -> 모든 정점)
back = dijkstra(k, v, graph);

//총 걸리는 시간 비교
for (int i = 1; i <= v; i++) {
answer = max(answer, go[i] + back[i]);
}
// 출력
cout << answer;
return 0;
}
86 changes: 86 additions & 0 deletions 13/15685.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
const int XY = 101;
typedef pair<int,int> p;

vector<vector<int>> v(XY, vector<int>(XY,0));
int x, y;

void move(int d, int g, int cnt, vector<int>& directions){
int n[4] = {1,0,-1,0};
int m[4] = {0,-1,0,1};

if(cnt>g) return;
if(cnt==0){ //0세대
v[x][y]=true;
x += n[d];
y += m[d];
v[x][y]=true;
directions.push_back(d);
cnt++;
move(d,g,cnt,directions);
}
else{ //n세대라면 (n>0)
int tmp=1, idx=directions.size();
//방향 벡터의 크기가 2^n가 될 때까지 방향 정보를 생성해준다.
//새로운 방향은 기존의 방향과 데칼코마니 형식으로 1씩 증가시켜준다.
//즉, 3세대고 초기 방향이 0이라면
// 0세대 : 0
// 1세대 : 0, 1
// 2세대 : 0, 1, 2, 1
// 3세대 : 0, 1, 2, 1, 2, 3, 2, 1
// 위와 같은 형식으로 방향 값을 구해 저장해주고 해당 방향으로 움직여 준다.
while(directions.size()!=pow(2,cnt)){
int d = directions[idx-tmp]+1;
if(d>3) d=0;
directions.push_back(d);
idx++;
tmp+=2;

x += n[d];
y += m[d];

if(x<=100 && y<=100 && x>=0 && y>=0) {
v[x][y]=true;
}
}
cnt++;
move(d,g,cnt,directions);
}
}

int checkAns(){ //(0,0)부터 출발해서 네 꼭짓점이 모두 true일 때 count 해준다.
int ans=0;
for(int i=0;i<XY-1;i++){
for(int j=0;j<XY-1;j++){
if(v[i][j]&&v[i+1][j]&&v[i][j+1]&&v[i+1][j+1]){
ans++;
}
}
}
return ans;
}

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

int n;
cin >> n;

while(n--){
int d,g;
cin >> x >> y >> d >> g;

vector<int> directions; //방향 정보를 저장한다.
move(d,g,0,directions);
}

cout << checkAns();

return 0;
}
60 changes: 60 additions & 0 deletions 13/2458.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int,int> p;
const int INF = 250001;

//최종적으로 양 쪽 중 하나라도 true인 경우만 count
//양방향으로 false라면 누가 우위인지 알 수 없다는 뜻
int count(int n, vector<vector<bool>>& v){
int cnt = 0;
for(int i=1;i<n+1;i++){
int flag = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. flag는 boolean 타입으로 하는 게 어떨까요?

for(int j=1;j<n+1;j++){
if(i==j) continue;
if(!v[i][j] && !v[j][i]){
flag=0;
break;
}
}
if(flag) cnt++;
}
return cnt;
}

void floydWarshall(int n,vector<vector<bool>>& v){
//v[1][5]=true, v[5][2]=true라면 v[1][2]도 true
for(int k=1;k<n+1;k++){ //중간
for(int i=1;i<n+1;i++){ //시작
for(int j=1;j<n+1;j++){ //끝
if(v[i][k] && v[k][j]){
v[i][j]=true;
}
}
}
}
}

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

int n,m;
cin >> n >> m;

//크기가 N * N인 이차원 벡터에 비교 결과를 bool형으로 저장한다.
vector <vector<bool>> v(n+1,vector<bool>(n+1,false));
while(m--){
int a, b;
cin >> a >> b;

v[a][b]=true; //a에서 b로 갈 수 있다면 true
}

floydWarshall(n,v);
cout << count(n,v);

return 0;
}