POJ 3613 Cow Relays (Floyd + + discretization !), Pojrelays

Source: Internet
Author: User

POJ 3613 Cow Relays (Floyd + + discretization !), Pojrelays


Cow Relays
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:5611   Accepted:2209

Description

For their physical fitness program,N(2 ≤N<1,000,000) cows have decided to run a relay race usingT(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 knowLengthiOf 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,NCows 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 exactlyNCow trails.

Input

* Line 1: Four space-separated integers:N,T,S, AndE
* Lines 2 ..T+ 1: LineI+ 1 describes trailIWith three space-separated integers:Lengthi,I1I, AndI2I

Output

* Line 1: A single integer that is the shortest distance from intersectionSTo intersectionEThat traverses exactlyNCow trails.

Sample Input

2 6 6 411 4 64 4 88 4 96 6 82 6 93 8 9

Sample Output

10

Source

USACO 2007 November Gold

Question link: http://poj.org/problem? Id = 3613

Calculate the shortest path from the start point s to the end point e through k edges.

Question Analysis: 01 the K-Power C = A ^ K, C [I] [j] of the adjacent matrix A indicates the number of paths from point I to Point j passing through K edges, Floyd uses an intermediate vertex k to update the distance between I and j each time.If the update is successful, it indicates that the gap between j and I is exactly the shortest path at. If n-1 Floyd is performed, It is the shortest path between I and j after n-1 points, there are n-1 vertices between I and j, that is, there are n edges. Because n is large, we consider using a matrix to quickly calculate the power. In addition, the t of this question is relatively small, but l1, l2 is relatively large, so it is discretization, because the maximum t is 100, so there are a maximum of 200 points after discretization


#include <cstdio>#include <cstring>int h[205], cnt;struct matrix{    int m[205][205];    matrix()    {        memset(m, 0x3f, sizeof(m));    }};matrix Floyd(matrix a, matrix b) {    matrix ans;    for(int k = 1; k <= cnt; k++)        for(int i = 1; i <= cnt; i++)            for(int j = 1; j <= cnt; j++)                if(ans.m[i][j] > a.m[i][k] + b.m[k][j])                    ans.m[i][j] = a.m[i][k] + b.m[k][j];    return ans;}matrix quickmod(matrix a, int k){    matrix ans = a;    while(k)    {        if(k & 1)            ans = Floyd(ans, a);        k >>= 1;        a = Floyd(a, a);    }    return ans;}int main(){    int n, t, s, e;    cnt = 1;    matrix ans;    scanf("%d %d %d %d", &n, &t, &s, &e);    memset(h, 0, sizeof(h));    for(int i = 0; i < t; i++)    {           int u, v, w;        scanf("%d %d %d", &w, &u, &v);        if(!h[u])            h[u] = cnt++;        if(!h[v])            h[v] = cnt++;        if(ans.m[h[u]][h[v]] > w)            ans.m[h[u]][h[v]] = ans.m[h[v]][h[u]] = w;    }    ans = quickmod(ans, n - 1);    printf("%d\n", ans.m[h[s]][h[e]]);}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.