diff --git a/13/1238.c++ b/13/1238.c++ new file mode 100644 index 0000000..75449f5 --- /dev/null +++ b/13/1238.c++ @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; +typedef pair ci; +const int INF = 2e5; //최대 경로 값 (간선의 수 * 가중치 최대값) + +vector dijkstra(int start, int v, vector>& graph) { + vector dist(v + 1, INF); // 각 정점까지의 최단 경로 저장 + priority_queue 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> graph(v + 1, vector(0)); // 인접 리스트 + while (e--) { + cin >> a >> b >> w; + graph[a].push_back({ b, w }); + } + // 연산 + // 가고 오는 시간을 다익스트라 알고리즘을 사용해 따로 구한다. + vector go(v + 1), back; + //파티 장소로 가는데 걸리는 시간 (모든 정점 -> 파티 장소) + for (int i = 1; i <= v; i++) { + vector 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; +} \ No newline at end of file diff --git a/13/15685.c++ b/13/15685.c++ new file mode 100644 index 0000000..d215d1e --- /dev/null +++ b/13/15685.c++ @@ -0,0 +1,86 @@ +#include +#include +#include + +using namespace std; +const int XY = 101; +typedef pair p; + +vector> v(XY, vector(XY,0)); +int x, y; + +void move(int d, int g, int cnt, vector& 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> n; + + while(n--){ + int d,g; + cin >> x >> y >> d >> g; + + vector directions; //방향 정보를 저장한다. + move(d,g,0,directions); + } + + cout << checkAns(); + + return 0; +} \ No newline at end of file diff --git a/13/2458.c++ b/13/2458.c++ new file mode 100644 index 0000000..f396642 --- /dev/null +++ b/13/2458.c++ @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; +typedef pair p; +const int INF = 250001; + +//최종적으로 양 쪽 중 하나라도 true인 경우만 count +//양방향으로 false라면 누가 우위인지 알 수 없다는 뜻 +int count(int n, vector>& v){ + int cnt = 0; + for(int i=1;i>& v){ + //v[1][5]=true, v[5][2]=true라면 v[1][2]도 true + for(int k=1;k> n >> m; + + //크기가 N * N인 이차원 벡터에 비교 결과를 bool형으로 저장한다. + vector > v(n+1,vector(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; +} \ No newline at end of file