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;
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • spss分析方法聚类分析_变量聚类分析

    spss分析方法聚类分析_变量聚类分析聚类分析是根据研究对象的特征,按照一定标准对研究对象进行分类的一种分析方法。下面我们主要从下面四个方面来解说:一、实际应用聚类分析的目标就是在相似的基础上收集数据来分类。聚类源于很多领域,包括数学,计算机科学,统计学,生物学和经济学。在不同的应用领域,很多聚类技术都得到了发展,这些技术方法被用作描述数据,衡量不同数据源间的相似性,以及把数据源分类到不同的簇中。商业上:聚类分析被用来发现不同的客户群,并且通过购买模式刻画不同的客户群的特征。聚类分析是细分市场的有效工具,同时也可用于研究消费者行为

    2022年10月18日
  • ccd视觉定位教程_正规CCD视觉定位系统工作原理[通俗易懂]

    ccd视觉定位教程_正规CCD视觉定位系统工作原理[通俗易懂]产品品牌CCD视觉定位系统发货城市-有效期至长期有效最小起订1产品单价面议深圳精科视觉科技有限公司成立于2011年底,是一家在视觉及自动化领域有着多年经验的科技公司,专业从事非标自动化机器视觉整套解决方案。公司集研发、销售、维护为一体,汇聚了一批追求卓越、勇于探索、敢于创新、在行业内具有丰富经验的工程技术人员,组建了一支专业、敬业的市场营销团队。激光打标技术具有以下的特点1、可对绝大多数金属或非金…

  • nhibernate的简单配置与使用

    nhibernate的简单配置与使用配置nhibernate的方式有两种,一种是通过xml文件的方式配置,还有就是通过class的方式配置。网上大多数是以xml的方式配置nhibernate,本文则已class的方式来配置,并通过IOC(依赖注入,本文以构造注入)的方式注册nhibernate。下面就以一个demo来说明配置、注入以及使用的方法。创建一个工程,在工程下添加三个项目。1、Web工程(demo采用的是MVC框架)…

  • PyCharm激活码永久有效PyCharm2017.2.6激活码教程-持续更新,一步到位

    PyCharm激活码永久有效PyCharm2017.2.6激活码教程-持续更新,一步到位PyCharm激活码永久有效2017.2.6激活码教程-Windows版永久激活-持续更新,Idea激活码2017.2.6成功激活

  • html5里的空心圆柱体,容积及空心圆柱体积.doc[通俗易懂]

    html5里的空心圆柱体,容积及空心圆柱体积.doc[通俗易懂]容积及空心圆柱体积高碑店中心小学段玉红教学目标:1、在巩固圆柱体积的计算公式的基础上,通过对实物的观察认识空心圆柱体(套管),知道各部分名称及之间的关系,掌握套管体积的计算公式。能正确计算套管的体积。2、在研究套管体积计算方法过程中,发现形体之间的关系,引导学生用原有知识解决新问题。培养学生知识迁移的能力。3、通过对计算体积方法的对比,体会有效提高计算正确率的最佳方法,进一步提高学生计算能力…

  • 在Win10下 用 Powershell 或 CMD 完成文件的 MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512 等哈希校验[通俗易懂]

    在Win10下 用 Powershell 或 CMD 完成文件的 MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512 等哈希校验[通俗易懂]文章目录前言CertUtil[选项]-hashfileInFile[HashAlgorithm]使用简单使用总结前言发现Windows10自带哈希校验工具CertUtil[选项]-hashfileInFile[HashAlgorithm]选项可以没有选项:-Unicode–以Unicode编写重定向输出-gmt–将时间显示为GMT-seconds–用秒和毫秒显示时间-v

发表回复

您的电子邮箱地址不会被公开。

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