您的位置:首页 > 其它

poj Cow Relays 矩阵乘法思想与floyd

2012-05-03 19:30 316 查看
Cow Relays

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 3507Accepted: 1388
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: N, T, S, 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

USACO 2007 November Gold
题目求i,j之间边数恰为N的最短路径(边可以重复走),我们知道线性代数中有:01邻接矩阵A的K次方C=A^K,C[i][j]表示i点到j点正好经过K条边的路径数。
而floyd则是每次使用一个中间点k去更新i,j之间的距离,那么更新成功表示i,j之间恰有一个点k时的最短路,如果做N次floyd那么不就是i,j之间借助N个点时的最短路了?
考虑当a[i][k]+a[k][j]<c[i][j]的时候,c[i][j]=a[i][k]+a[k][j],这样c[i][j]保存了i,j之间有一个点的最短路,第二次将c[i][j]拷贝回到a[i][j]当中,并将c[i][j]重新置为INF,再做一次,则是在原来的基础上在i,j之间再用一个点k来松弛,这时候i,j之间实际上已经是两个点了,之后重复这么做就好了,可以利用二进制加速
贴一个网上的code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 1000000001
#define maxn 1010
int K,M,S,T;
int v[maxn],cnt,map[maxn][maxn],used[maxn];
int ans[maxn][maxn],dis[maxn][maxn],tmp[maxn][maxn];
void floyd(int c[][maxn],int a[][maxn],int b[maxn][maxn])
{
int i,j,k;
for(k=0;k<cnt;k++){
for(i=0;i<cnt;i++){
for(j=0;j<cnt;j++){
if(c[v[i]][v[j]]>a[v[i]][v[k]]+b[v[k]][v[j]])
c[v[i]][v[j]]=a[v[i]][v[k]]+b[v[k]][v[j]];
}
}
}
}
void copy(int a[][maxn],int b[][maxn])
{
int i,j;
for(i=0;i<cnt;i++)
{
for(j=0;j<cnt;j++)
{
a[v[i]][v[j]]=b[v[i]][v[j]];
b[v[i]][v[j]]=inf;
}
}
}
void solve(int k)
{
while(k)
{
if(k%2)
{
floyd(dis,ans,map); //有1的位加到ans
copy(ans,dis);
}
floyd(tmp,map,map); // 1,2,4,8......
copy(map,tmp);
k=k/2;
}
}
int main()
{
int i,j;
int x,y,val;
while(scanf("%d%d%d%d",&K,&M,&S,&T)==4)
{
for(i=0;i<=1001;i++)
{
for(j=0;j<=1001;j++)
{
map[i][j]=inf;
ans[i][j]=inf;
tmp[i][j]=inf;
dis[i][j]=inf;
}
ans[i][i]=0;
}
memset(used,0,sizeof(used));
cnt=0;
for(i=0;i<M;i++)
{
scanf("%d%d%d",&val,&x,&y);
if(map[x][y]>val)
{
map[x][y]=val;
map[y][x]=map[x][y];
}
if(!used[x])
{
used[x]=1;
v[cnt++]=x;
}
if(!used[y])
{
used[y]=1;
v[cnt++]=y;
}
}
solve(K);
printf("%d\n",ans[S][T]);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: