Til the Cows Come Home
POJ - 2387
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
1 import java.util.*;
2
3 class node implements Comparable<node>{
4 public int pos;
5 public int length;
6 public node() {
7 this.pos = pos;
8 this.length = length;
9 }
10 public int compareTo(node x) {
11 return length-x.length;
12 }
13 }
14 public class Main {
15 static int maxn = 100010;
16 static int[] vis = new int [maxn];
17 static PriorityQueue<node> q = new PriorityQueue<node>();
18 static ArrayList<node>g[] = new ArrayList[maxn];
19 static int[] ans= new int [maxn];
20 static int[] dis = new int [maxn];
21 int cnt = 0;
22 static void dij(int st,int ed) {
23 node t = new node();
24 t.pos = st;
25 t.length = 0;
26 q.offer(t);
27 dis[st] = 0;
28 vis[st] = 1;
29 while(!q.isEmpty()) {
30 node uNode = new node();
31 uNode = q.poll();
32 //System.out.println(uNode.length);
33 vis[uNode.pos] = 0;
34 int now = uNode.pos;
35 //System.out.println("now="+g[now].size());
36 for(int i=0;i<g[now].size();i++) {
37 node aa = new node();
38 aa = g[now].get(i);
39 //System.out.println(aa.pos);
40 int v = aa.pos;
41 int len = aa.length;
42 if(dis[v]>dis[uNode.pos]+len) {
43 dis[v] = dis[uNode.pos]+len;
44 if(vis[v]==0)
45 vis[v] = 1;
46 aa.pos = v;
47 aa.length = dis[v];
48 //System.out.println(aa.length+" "+aa.pos);
49 q.offer(aa);
50 }
51 }
52
53 }
54 System.out.println(dis[ed]);
55 }
56 public static void main(String[] args) {
57 Scanner cin = new Scanner(System.in);
58 int n,m,a,b,p,d;
59 while(cin.hasNext()) {
60 n = cin.nextInt();
61 m = cin.nextInt();
62 for(int i=0;i<maxn;i++)
63 {
64 g[i]=new ArrayList<node>();
65 }
66 for(int i=0;i<maxn;i++)
67 dis[i] = 10001000;
68 for(int i=0;i<n;i++) {
69 a = cin.nextInt();
70 b = cin.nextInt();
71 d = cin.nextInt();
72 node tmp = new node();
73 tmp.pos = b;
74 tmp.length = d;
75
76 g[a].add(tmp);
77 tmp.pos = a;
78 tmp.length = d;
79 g[b].add(tmp);
80 }
81 dij(m,1);
82 }
83 }
84 }
1 import java.util.Scanner;
2
3 public class Main{
4 static long mod = 1000000007;
5 static node a,b = new node();
6 static void init() {
7 a = new node();
8 a.m[0][0] = 1;
9 a.m[0][1] = -1;
10 a.m[1][0] = 1;
11 a.m[1][1] = 0;
12 for(int i=0;i<2;i++)
13 b.m[i][i] = 1;
14 }
15 static node mul(node aa,node bb) {
16 node c = new node();
17 for(int i=0;i<2;i++) {
18 for(int j=0;j<2;j++) {
19 c.m[i][j] = 0;
20 for(int k=0;k<2;k++) {
21 c.m[i][j] += (aa.m[i][k] * bb.m[k][j]) % mod;
22 }
23 c.m[i][j]%=mod;
24 }
25 }
26 return c;
27 }
28 static node powmod(node aa,node bb,int t) {
29 while(t!=0) {
30 if(t%2==1) {
31 bb = mul(aa, bb);
32 }
33 aa = mul(aa, aa);
34 t>>=1;
35 }
36 return bb;
37 }
38 public static void main(String[] args) {
39 long [] f = new long[4];
40 Scanner cin = new Scanner(System.in);
41 f[0] = cin.nextLong();
42 f[1] = cin.nextLong();
43 int l = cin.nextInt();
44 init();
45 if(l==1) {
46 System.out.println((f[0]%mod+mod)%mod);
47 }
48 else if(l==2) {
49 System.out.println((f[1]%mod+mod)%mod);
50 }
51 else {
52 node res = new node();
53 res = powmod(a, b, l-2);
54 long ans = 0;
55 ans = (((res.m[0][0]*f[1]+res.m[0][1]*f[0])%mod)+mod)%mod;
56 System.out.println(ans);
57 }
58 }
59
60 }
61
62 class node{
63 long [][] m = new long [5][5];
64 }