POJ3613 Cow Relays

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6726   Accepted: 2626

Description

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.

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.

Input

* Line 1: Four space-separated integers: NTS, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

Sample Input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

Source

 

经过n条边的最短路径。

好像可以贪心,先找到一条最短路,然后随便找个最小环绕圈圈,我没有尝试。

 

使用了标准解法,矩阵乘法+倍增floyd

floyd是枚举k点,更新i到j的最短路径,更新后实际上就是从原本的走一条路到达更新成了走两条路到达。

根据这个思想,我们可以倍增跑floyd,就能得出走2^k条边从i到j的最短路径。

 

代码如下:(a[i]里存的就是走2^i条边的最短路邻接矩阵)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 const int mxn=200;
 8 int mp[2000],cnt=0;
 9 int m;
10 struct edge{
11     int x,y;
12     int len;
13 }e[mxn];
14 struct M{
15     int v[200][200];
16     M(){memset(v,50,sizeof v);}
17     friend M operator *(M a,M b){
18         M c;
19         int i,j,k;
20         for(i=0;i<=m;i++)
21           for(j=0;j<=m;j++)
22             for(k=0;k<=m;k++){
23                 c.v[i][j]=min(c.v[i][j],a.v[i][k]+b.v[k][j]);
24             }
25         return c;
26     }
27 }a[120];
28 int n,t,S,E;
29 int main(){
30     scanf("%d%d%d%d",&n,&t,&S,&E);
31     int i,j;
32     memset(mp,-1,sizeof mp);
33     int u,v;
34     for(i=1;i<=t;i++){
35         scanf("%d%d%d",&e[i].len,&u,&v);
36         if(mp[u]==-1)mp[u]=++cnt;
37         if(mp[v]==-1)mp[v]=++cnt;
38         e[i].x=mp[u];e[i].y=mp[v];
39     }
40     for(i=1;i<=t;i++){
41         int x=e[i].x;int y=e[i].y;
42         a[0].v[x][y]=a[0].v[y][x]=e[i].len;
43     }
44     if(mp[S]==-1)mp[S]=++cnt;
45     if(mp[E]==-1)mp[E]=++cnt;
46     m=cnt;
47     S=mp[S];E=mp[E];
48     for(i=1;i<=20;i++){//倍增 
49         a[i]=a[i-1]*a[i-1];
50     }
51     M ans=a[0];
52     n--;
53     for(i=20;i>=0;i--){
54         if((1<<i)&n)ans=ans*a[i];
55     }
56     printf("%d\n",ans.v[S][E]);
57     return 0;
58 }

 

posted @ 2016-07-12 22:35  SilverNebula  阅读(240)  评论(0编辑  收藏  举报
AmazingCounters.com