poj 3613 Cow Relays

poj 3613 Cow Relays

大家好,又见面了,我是全栈君。

Cow Relays
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5411   Accepted: 2153

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

n次floyd,原来flody也能够像矩阵一样高速幂。详细的能够看论文《矩阵乘法在信息学的应用》

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int inf=(1<<30)-1;
int n;
int hash[3010];//映射
struct matrix
{
    int ma[210][210];
    matrix()
    {
       for(int i=0;i<210;i++)
          for(int j=0;j<210;j++)
           ma[i][j]=inf;
    }
};
matrix floyd(matrix x,matrix y)
{
    matrix ans;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=n;k++)
            ans.ma[i][j]=min(ans.ma[i][j],x.ma[i][k]+y.ma[k][j]);
        }
    }
    return ans;
}
matrix pow(matrix a,int m)
{
    matrix ans;
    for(int i=1;i<=n;i++)
    ans.ma[i][i]=0;
    while(m)
    {
        if(m&1)
        ans=floyd(ans,a);
        a=floyd(a,a);
        m=m>>1;
    }
    return ans;
}
int main()
{
    int k,t,s,e;
    while(~scanf("%d%d%d%d",&k,&t,&s,&e))
    {
        int x,y,d;
        memset(hash,0,sizeof(hash));
        n=1;
        matrix a;
        for(int i=0;i<t;i++)
        {
            scanf("%d%d%d",&d,&x,&y);
            if(!hash[x])
            hash[x]=n++;
            if(!hash[y])
            hash[y]=n++;
            a.ma[hash[x]][hash[y]]=a.ma[hash[y]][hash[x]]=d;
        }
        n=n-1;
        matrix ans;
        ans=pow(a,k);
        printf("%d\n",ans.ma[hash[s]][hash[e]]);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115421.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql 基础

    mysql 基础

    2021年8月18日
    72
  • 损失函数与代价函数区别

    损失函数与代价函数区别各种损失函数的优缺点详解损失函数或者代价函数的目的是:衡量模型的预测能力的好坏。损失函数(Lossfunction):是定义在单个训练样本上的,也就是就算一个样本的误差,比如我们想要分类,就是预测的类别和实际类别的区别,是一个样本的哦,用L表示。代价函数(Costfunction):是定义在整个训练集上面的,也就是所有样本的误差的总和的平均,也就是损失函数的总和的平均,有没有这个平…

    2022年5月12日
    62
  • 【收藏】FFmpeg从入门到精通——进阶篇,SEI那些事儿

    【收藏】FFmpeg从入门到精通——进阶篇,SEI那些事儿原文链接:SEI 

    2022年6月26日
    35
  • oracle数据库定义变量和使用_oracle执行变量

    oracle数据库定义变量和使用_oracle执行变量一、异常错误介绍我们在使用oracle数据库做程序开发时,一般都会使用plsql做客户端连接查询工具,在写sql语句时plsql经常会报并非所有变量都已绑定01008这样类似的异常错误,通常我们程序员还看不出具体有什么毛病,具体错误提示见下图显示:出现以上这种错误出现的次数多了,我们就会有经验解决了,经过我们常年的工作经验以及网友的问题汇总,得出的最终结论就是:程序员sql语句书写不严谨导致该问题…

    2025年9月30日
    6
  • J2EE是什么意思_main()函数是java程序的执行入口

    J2EE是什么意思_main()函数是java程序的执行入口j2ee   J2EE简介  J2EEJava2平台企业版(Java2Platform,EnterpriseEdition)   J2EE是一套全然不同于传统应用开发的技术架构,包含许多组件,主要可简化且规范应用系统的开发与部署,进而提高可移植性、安全与再用价值。   J2EE核心是一组技术规范与指南,其中所包含的各类组件、服务架构及技术层次,均有共通的标准及规格,让各种依

    2022年10月11日
    2
  • mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」

    mysql的存储过程介绍、创建、案例、删除、查看「建议收藏」mysql的存储过程介绍、创建、案例、删除、查看

    2022年4月23日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号