โจ ์ต๋จ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ โก๏ธ ๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ
https://www.acmicpc.net/problem/11657
11657๋ฒ: ํ์๋จธ์
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ N (1 โค N โค 500), ๋ฒ์ค ๋ ธ์ ์ ๊ฐ์ M (1 โค M โค 6,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์๋ ๋ฒ์ค ๋ ธ์ ์ ์ ๋ณด A, B, C (1 โค A, B โค N, -10,000 โค C โค 10,000)๊ฐ ์ฃผ์ด์ง๋ค.
www.acmicpc.net
๐ ๊ณ ๋ คํด์ผํ ์
- C < 0 ์ธ ๊ฒฝ์ฐ ํ์๋จธ์ ์ผ๋ก ์๊ฐ์ ๋๋์๊ฐ๋ ๊ฒฝ์ฐ์ด๋ค. โ ์์ ๊ฐ์ค์น ์กด์ฌ
- 1๋ฒ ๋์์์ ์ถ๋ฐํ๋ค.
- ์์ ์ฌ์ดํด์ด ์๊ฑฐ๋ ๊ฒฝ๋ก๊ฐ ์์ ์๋ค๋ฉด -1 ์ถ๋ ฅ
- ์ค๋ฒํ๋ก์ฐ ๋ฒ์ ์กฐ์ฌ
๐น๏ธ ํ์ด๊ณผ์
1๏ธโฃ ๊ฐ์ ๊ณผ ์ ์ , ๋น์ฉ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
2๏ธโฃ ๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด์ ์ต๋จ ์๊ฐ์ ๊ตฌํ๋ค.
3๏ธโฃ ์์ ์ฌ์ดํด์ด ์กด์ฌํ๋ค๋ฉด -1์ ์ถ๋ ฅํ๋ค.
๐ฉโ๐ป ์ ์ฒด ์ฝ๋
public class BOJ_11657_ํ์๋จธ์ {
static int N, M;
static ArrayList<Info>[] bus;
static long[] dist;
static int INF = 1000000000;
static class Info{
int to, time;
public Info(int to, int time) {
this.to = to;
this.time = time;
}
}
static boolean bellmanFord(){
dist[1] = 0;
for (int i = 1; i < N; i++) {
for (int j = 1; j < bus.length; j++) {
for (Info next : bus[j]) {
if (dist[j] == INF) break;
if (dist[next.to] > next.time + dist[j]) {
dist[next.to] = next.time + dist[j];
}
}
}
}
for (int j = 1; j < bus.length; j++) {
for (Info next : bus[j]) {
if(dist[j] == INF) break;
if (dist[next.to] > next.time + dist[j]) {
return true; // ์์ ์ฌ์ดํด ์กด์ฌ
}
}
}
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
bus = new ArrayList[N + 1];
dist = new long[N + 1];
Arrays.fill(dist, INF);
for(int i=1; i<=N; i++){
bus[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
bus[a].add(new Info(b, c));
}
StringBuilder sb = new StringBuilder();
if (bellmanFord()) { // ์์ ์ฌ์ดํด์ด ์์ผ๋ฉด -1 ์ถ๋ ฅํ๊ณ ๋
sb.append("-1");
} else {
for (int i = 2; i <= N; i++) {
// ์์ ์ฌ์ดํด์ด ์์ผ๋ฉด ์ต๋จ ๊ฒฝ๋ก ์ถ๋ ฅํ๊ณ , ๊ฒฝ๋ก๊ฐ ์์ ์์ผ๋ฉด -1 ์ถ๋ ฅ
if (dist[i] == INF) {
sb.append(-1 + "\n");
}else{
sb.append(dist[i] + "\n");
}
}
}
System.out.println(sb);
}
}
'์๊ณ ๋ฆฌ์ฆ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 4179 ๋ถ! - Java (1) | 2023.10.25 |
---|---|
[BOJ] 14502 ์ฐ๊ตฌ์ - Java (0) | 2023.09.28 |
[BOJ] 3190 ๋ฑ (0) | 2023.09.25 |
[BOJ] 1865 ์ํ (0) | 2023.09.08 |
[BOJ] 1939 ์ค๋์ ํ (0) | 2023.09.07 |
โจ ์ต๋จ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ โก๏ธ ๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ
https://www.acmicpc.net/problem/11657
11657๋ฒ: ํ์๋จธ์
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ N (1 โค N โค 500), ๋ฒ์ค ๋ ธ์ ์ ๊ฐ์ M (1 โค M โค 6,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์๋ ๋ฒ์ค ๋ ธ์ ์ ์ ๋ณด A, B, C (1 โค A, B โค N, -10,000 โค C โค 10,000)๊ฐ ์ฃผ์ด์ง๋ค.
www.acmicpc.net
๐ ๊ณ ๋ คํด์ผํ ์
- C < 0 ์ธ ๊ฒฝ์ฐ ํ์๋จธ์ ์ผ๋ก ์๊ฐ์ ๋๋์๊ฐ๋ ๊ฒฝ์ฐ์ด๋ค. โ ์์ ๊ฐ์ค์น ์กด์ฌ
- 1๋ฒ ๋์์์ ์ถ๋ฐํ๋ค.
- ์์ ์ฌ์ดํด์ด ์๊ฑฐ๋ ๊ฒฝ๋ก๊ฐ ์์ ์๋ค๋ฉด -1 ์ถ๋ ฅ
- ์ค๋ฒํ๋ก์ฐ ๋ฒ์ ์กฐ์ฌ
๐น๏ธ ํ์ด๊ณผ์
1๏ธโฃ ๊ฐ์ ๊ณผ ์ ์ , ๋น์ฉ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
2๏ธโฃ ๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด์ ์ต๋จ ์๊ฐ์ ๊ตฌํ๋ค.
3๏ธโฃ ์์ ์ฌ์ดํด์ด ์กด์ฌํ๋ค๋ฉด -1์ ์ถ๋ ฅํ๋ค.
๐ฉโ๐ป ์ ์ฒด ์ฝ๋
public class BOJ_11657_ํ์๋จธ์ {
static int N, M;
static ArrayList<Info>[] bus;
static long[] dist;
static int INF = 1000000000;
static class Info{
int to, time;
public Info(int to, int time) {
this.to = to;
this.time = time;
}
}
static boolean bellmanFord(){
dist[1] = 0;
for (int i = 1; i < N; i++) {
for (int j = 1; j < bus.length; j++) {
for (Info next : bus[j]) {
if (dist[j] == INF) break;
if (dist[next.to] > next.time + dist[j]) {
dist[next.to] = next.time + dist[j];
}
}
}
}
for (int j = 1; j < bus.length; j++) {
for (Info next : bus[j]) {
if(dist[j] == INF) break;
if (dist[next.to] > next.time + dist[j]) {
return true; // ์์ ์ฌ์ดํด ์กด์ฌ
}
}
}
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
bus = new ArrayList[N + 1];
dist = new long[N + 1];
Arrays.fill(dist, INF);
for(int i=1; i<=N; i++){
bus[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
bus[a].add(new Info(b, c));
}
StringBuilder sb = new StringBuilder();
if (bellmanFord()) { // ์์ ์ฌ์ดํด์ด ์์ผ๋ฉด -1 ์ถ๋ ฅํ๊ณ ๋
sb.append("-1");
} else {
for (int i = 2; i <= N; i++) {
// ์์ ์ฌ์ดํด์ด ์์ผ๋ฉด ์ต๋จ ๊ฒฝ๋ก ์ถ๋ ฅํ๊ณ , ๊ฒฝ๋ก๊ฐ ์์ ์์ผ๋ฉด -1 ์ถ๋ ฅ
if (dist[i] == INF) {
sb.append(-1 + "\n");
}else{
sb.append(dist[i] + "\n");
}
}
}
System.out.println(sb);
}
}
'์๊ณ ๋ฆฌ์ฆ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 4179 ๋ถ! - Java (1) | 2023.10.25 |
---|---|
[BOJ] 14502 ์ฐ๊ตฌ์ - Java (0) | 2023.09.28 |
[BOJ] 3190 ๋ฑ (0) | 2023.09.25 |
[BOJ] 1865 ์ํ (0) | 2023.09.08 |
[BOJ] 1939 ์ค๋์ ํ (0) | 2023.09.07 |