For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.ios
Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.算法
To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.數組
Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.ide
給出一張無向連通圖,求S到E通過k條邊的最短路。spa
輸入格式:code
* Line 1: Four space-separated integers: N, T, S, and Eorm
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2iblog
輸出格式:three
* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.ci
2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9
10
題解:
一句話題意:給出一個有t條邊的圖,求從s到e剛好通過k條邊的最短路。
a:是圖的鄰接矩陣,f是圖中任意兩點直接的最短距離、
floyd算法:
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
一遍floyed後: f[i][j]是i到j的最短距離:中間至少1條邊(連通圖),最多n-1條邊
floyd算法的變形:
a:是圖的鄰接矩陣,a[i][j]是通過一條邊的最短路徑。f[i][j]k的初值爲∞
f[i][j]-1=∞;
f[i][j]1=a; //通過一條邊的最短路徑
f[i][j]2=min(f[i][j]2,a[i][k]+a[k][j])=a*a=a2;//通過二條邊的最短路徑,通過一次floyd.矩陣相乘一次,f[i][j]2初值爲∞。
f[i][j]3=min(f[i][j]3,f[i][k]2+a[k][j])=a*a*a=a3;//通過三條邊的最短路徑,通過二次floyd。f[i][j]3初值爲∞
f[i][j]4=min(f[i][j]4,f[i][k]3+a[k][j])=a4;//通過四條邊的最短路徑,通過三次floyd,f[i][j]4初值爲∞
...
f[i][j]k=min(f[i][j]k,f[i][k]k-1+a[k][j])=ak;//通過k條邊的最短路徑,通過K-1次floyd,f[i][j]k初值爲∞
而floyd的時間複雜度爲O(n3),則從的時間複雜度爲O(Kn3),很是容易超時。
因此咱們能夠用快速冪來完成。
f[i][j]r+p=min(f[i][j]r+p,f[i][k]r+f[k][j]p)
程序:
//洛谷2886 //(1)對角線不能設置爲0,不然容易自循環。(2)數組要放在主程序外面 //(3)K條邊,不必定是最簡路 #include<iostream> #include<cstdio> #include<map> #include<cstring> using namespace std; map<int,int>f; const int maxn=210; int k,t,s,e,n; int a[maxn][maxn]; struct Matrix{ int b[maxn][maxn]; }; Matrix A,S; Matrix operator *(Matrix A,Matrix B){//運算符重載 Matrix c; memset(c.b,127/3,sizeof(c.b) ); for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) c.b[i][j]=min(c.b[i][j],A.b[i][k]+B.b[k][j]); return c; } Matrix power(Matrix A,int k){ if(k==0) return A; Matrix S=A; for(int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<A.b[i][j]<<" "; cout<<endl; } cout<<endl; while(k){ if(k&1)S=S*A;//奇數執行,偶數不執行 /*cout<<k<<":"<<endl; for(int i=1;i<=n;i++){ for (int j=1;j<=n;j++) cout<<S.b[i][j]<<" "; cout<<endl; }*/ cout<<endl; A=A*A; k=k>>1; } return S; } int main(){ cin>>k>>t>>s>>e; int w,x,y; memset(A.b,127/3,sizeof(A.b) );// 賦初值 n=0; for(int i=1;i<=t;i++){//t<100,x<1000 點不是從1開始的,能夠用map離散化,給點從1開始編號。n記錄共有多少個點。 cin>>w>>x>>y; if (f[x]==0) f[x]=++n; if (f[y]==0) f[y]=++n; A.b[f[x]][f[y]]=A.b[f[y]][f[x]]=min(w,A.b[f[y]][f[x]]); } S=power(A,k-1);//快速冪.執行k-1次floyd矩陣乘 s=f[s]; e=f[e]; cout<<S.b[s][e]; return 0; }