java写最短路和矩阵快速幂

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.

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.

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.

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 }

原文地址:https://www.cnblogs.com/1013star/p/10353221.html

时间: 2024-08-12 21:54:03

java写最短路和矩阵快速幂的相关文章

poj 3613 经过k条边最短路 floyd+矩阵快速幂

http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路,跑k-1次"floyd"即可(使用矩阵快速幂的思想). 把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j.令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点).类似地,C*A的第i行第j列就

【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. 在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他又和他人讨论起了二叉搜索树.什么是二叉搜索树呢?二叉搜索树首先是一棵二叉树.设key[p]表示结点p上的数值.对于其中的每个结点p,若其存在左孩子lch,则key

POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu 5667 Sequence【矩阵快速幂】

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 285    Accepted Submission(s): 92 Problem Description Holion August will eat every thing he has found. Now there are many foods,but he d

BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn alg

ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description You might have noticed that there is the new fashion among rich people to have their yards tiled with black and white tile

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t