Input
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
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 }
原文:https://www.cnblogs.com/1013star/p/10353221.html