您的位置:首页 > 其它

POJ 3613 Cow Relays Floyd最短路

2013-08-07 13:35 253 查看
Cow Relays

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4590Accepted: 1823
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
---------------------

求点s到点e经过k条边的最短路。

类似floyd的思想。+矩阵优化

------

/** head-file **/

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <algorithm>

/** define-for **/

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)

/** define-useful **/

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) int(x.size())
#define see(x) cerr<<#x<<" "<<x<<endl
#define se(x) cerr<<" "<<x
#define pb push_back
#define mp make_pair

/** test **/

#define Display(A, n, m) {                      \
    REP(i, n){                                  \
        REP(j, m) cout << A[i][j] << " ";       \
        cout << endl;                           \
    }                                           \
}

#define Display_1(A, n, m) {                    \
    REP_1(i, n){                                \
        REP_1(j, m) cout << A[i][j] << " ";     \
        cout << endl;                           \
    }                                           \
}

using namespace std;

/** typedef **/

typedef long long LL;

/** Add - On **/

const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0); //M_PI;
const int maxsize=100;
const int maxn=11111;
struct Matrix
{
    int element[maxsize][maxsize];
    int size;
    Matrix(int n=0){
        clr(element,-1);
        size=n;
    }
};

Matrix FloydMul(Matrix A,Matrix B)
{
    int tmp,size;
    size=A.size;
    Matrix C(size);
    for (int i=0;i<size;i++){
        for (int j=0;j<size;j++){
            for (int k=0;k<size;k++){
                if (A.element[i][k]==-1||B.element[k][j]==-1) continue;
                tmp=A.element[i][k]+B.element[k][j];
                if (C.element[i][j]==-1||C.element[i][j]>tmp) C.element[i][j]=tmp;
            }
        }
    }
    return C;
}

Matrix FloydPow(Matrix A,int exp)  {
    Matrix tmp = FloydMul(A,A);
    if (exp==1) return A;
    else if (exp&1) return FloydMul(FloydPow(tmp,exp/2),A);
    else return FloydPow(tmp,exp/2);
}

int hash[maxn];
int h;
int n,m,s,e;
int main()
{
    while (~scanf("%d%d%d%d",&n,&m,&s,&e))
    {
        clr(hash,-1);
        h=0;
        Matrix A;
        REP(i,m)
        {
            int x,y,w;
            scanf("%d%d%d",&w,&x,&y);
            if (hash[x]==-1) hash[x]=h++;
            if (hash[y]==-1) hash[y]=h++;
            A.element[hash[x]][hash[y]]=w;
            A.element[hash[y]][hash[x]]=w;
        }
        A.size=h;
        Matrix R=FloydPow(A,n);
        printf("%d\n",R.element[hash[s]][hash[e]]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: