| Task: | Flowery Trails |
| Sender: | TEAM-Patonki |
| Submission time: | 2017-09-26 18:48:04 +0300 |
| Language: | Java |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.15 s | details |
| #2 | ACCEPTED | 0.13 s | details |
| #3 | ACCEPTED | 0.55 s | details |
| #4 | ACCEPTED | 0.49 s | details |
| #5 | ACCEPTED | 0.67 s | details |
| #6 | ACCEPTED | 0.49 s | details |
| #7 | ACCEPTED | 0.47 s | details |
| #8 | ACCEPTED | 0.47 s | details |
| #9 | ACCEPTED | 0.47 s | details |
| #10 | ACCEPTED | 0.49 s | details |
Code
//import java.util.ArrayList;
//import java.util.Arrays;
//
//public class Kilpalaatikko {
//
// public static void main(String[] args) {
// IO io = new IO();
// int n = io.nextInt();
// long[] table = new long[n];
// for (int i = 0; i < n; i++) {
// table[i] = io.nextLong();
// }
// Arrays.sort(table);
// boolean poss = false;
// for (int i = 2; i < n; i++) {
// if (table[i] < table[i - 1] + table[i - 2]) {
// poss = true;
// }
// }
//
// if (poss) {
// io.print("possible");
// } else {
// io.print("impossible");
// }
//
// io.close(); // TÄYTYY KUTSUA LOPUKSI, muuten tuloste voi jäädä kirjoittamatta
//
// }
//
//}
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.Queue;
public class Kilpalaatikko {
public static void main(String[] args) {
IO io = new IO();
int n = io.nextInt();
int m = io.nextInt();
ArrayList<pari>[] arraylist = new ArrayList[n + 1];
ArrayList<pari>[] arraylist2 = new ArrayList[n + 1];
for (int i = 0; i < n + 1; i++) {
arraylist[i] = new ArrayList<>();
arraylist2[i] = new ArrayList<>();
}
Queue<pari> jono = new PriorityQueue<pari>();
for (int i = 0; i < m; i++) {
int a = io.nextInt();
int b = io.nextInt();
int c = io.nextInt();
arraylist[a].add(new pari(b, c, 0));
arraylist[b].add(new pari(a, c, 0));
}
boolean[] visited = new boolean[n + 1];
int[] d = new int[n + 1];
for (int i = 0; i < n + 1; i++) {
d[i] = Integer.MAX_VALUE;
}
int[] monta = new int[n + 1];
monta[1] = 1;
d[1] = 0;
jono.add(new pari(1, 0, 0));
while (!jono.isEmpty()) {
pari aa = jono.poll();
if (aa.y == d[aa.x]) {
monta[aa.x] += monta[aa.z];
arraylist2[aa.x].add(new pari(aa.z, aa.y - d[aa.z], 0));
}
if (visited[aa.x]) {
continue;
}
visited[aa.x] = true;
for (pari pp : arraylist[aa.x]) {
if (!visited[pp.x]) {
jono.add(new pari(pp.x, d[aa.x] + pp.y, aa.x));
if (d[pp.x] >= pp.y + d[aa.x]) {
d[pp.x] = pp.y + d[aa.x];
}
}
}
// if(d[n] < Integer.MAX_VALUE){
// break;
// }
}
long tulos = 0;
Queue<pari> jono2 = new PriorityQueue();
jono2.add(new pari(n, 0, 0));
boolean[] visited2 = new boolean[n + 1];
while (!jono2.isEmpty()) {
pari pp = jono2.poll();
if (pp.x == 1) {
continue;
}
for (pari xx : arraylist2[pp.x]) {
tulos += xx.y;
if (!visited2[xx.x]) {
jono2.add(new pari(xx.x, 0, 0));
visited2[xx.x] = true;
}
}
}
io.print(tulos * 2);
io.close(); // TÄYTYY KUTSUA LOPUKSI, muuten tuloste voi jäädä kirjoittamatta
}
}
class pari implements Comparable<pari> {
int x;
int y;
int z;
public pari(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public int compareTo(pari o) {
return -o.y + this.y;
}
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 15 1 2 580 2 5 90 2 5 90 5 10 250 ... |
| correct output |
|---|
| 3860 |
| user output |
|---|
| 3860 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 4 7 1 2 1 1 3 2 1 4 10 1 4 3 ... |
| correct output |
|---|
| 18 |
| user output |
|---|
| 18 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 500 249500 1 2 1 1 2 1 1 3 1000 1 3 1000 ... |
| correct output |
|---|
| 1996 |
| user output |
|---|
| 1996 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 500 249500 1 2 1 1 2 10 1 3 999 1 3 1000 ... |
| correct output |
|---|
| 998 |
| user output |
|---|
| 998 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 4096 245680 1 2 1 1 3 1 2 4 1 2 5 1 ... |
| correct output |
|---|
| 491360 |
| user output |
|---|
| 491360 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 5000 246585 1 2 1 1 3 1 2 4 1 2 5 1 ... |
| correct output |
|---|
| 12284 |
| user output |
|---|
| 12284 |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 10000 250000 966 4849 16 6751 3592 929 5263 5263 21 3001 6112 852 ... |
| correct output |
|---|
| 76 |
| user output |
|---|
| 76 |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 9999 250000 9488 9488 958 3806 3806 742 4895 4600 223 9024 9024 799 ... |
| correct output |
|---|
| 202 |
| user output |
|---|
| 202 |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 9999 250000 9396 4472 7 7056 45 7 6747 7491 5 4382 5641 5 ... |
| correct output |
|---|
| 82 |
| user output |
|---|
| 82 |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 10000 250000 5779 9141 578 6851 288 305 7139 2759 834 4526 8082 748 ... |
| correct output |
|---|
| 422 |
| user output |
|---|
| 422 |
