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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java是什么?主要是干什么的?「建议收藏」

    Java是什么?主要是干什么的?「建议收藏」随着Java技术不断发展,许多人都想问:Java是什么?主要是干什么的呀?现在小朗来为大家解惑。java是一种高级计算机语言,一种可以编写跨平台应用软件、完全面向对象的程序设计语言。那Java主要是干嘛的呀?一、java可以做网站Java主要可以用于编写网站,如今许多商业网站都用Jsp写的,JSP全称JavaServerPages。它是一种动态网站技术性,例如大家了解的163,一些政府门户网站全是选用JSP撰写的。因此学习培训Java的同学们能够找开发网站层面的工作中,并且…

    2022年7月7日
    22
  • git的下载与安装(手机原装计算器下载安装)

    1首先,进入Git的官网:git–fast-version-control如上图所示,在Git的官网中点击Downloads,进入如下页面:根据操作系统选择合适的版本:2 默认一步步安装即可  需要注意:到下图所示步骤时,建议选择第二项3 验证是否成功: 鼠标右键单击 GitBashhere 并在窗口输入git,出现以下信息:…

    2022年4月17日
    43
  • navicat2021激活码【中文破解版】

    (navicat2021激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html3Y…

    2022年3月30日
    47
  • Spring笔记(3)

    Spring笔记(3)

    2021年11月11日
    42
  • Tarjan_com.pakdata.QuranMajeed

    Tarjan_com.pakdata.QuranMajeedTarjanTarjan是一种求有向图强联通分量的算法,是用dfs实现以及时间戳标记访问最短时间的.Tarjan算法中每个点都需要扩展边,为了存储方便,推荐使用邻接表.Tarjan算法的优势在于其灵活性,基础代码可以直接适用于多数情况.常见于dfs序.

    2022年10月27日
    0
  • TCP常用网络和木马使用端口对照表,常用和不常用端口一览表

    TCP常用网络和木马使用端口对照表,常用和不常用端口一览表【开始-运行-CMD,输入netstat-an然后回车就可以查看端口】    端口:0  服务:Reserved  说明:通常用于分析操作系统。这一方法能够工作是因为在一些系统中“0”是无效端口,当你试图使用通常的闭合端口连接它时将产生不同的结果。一种典型的扫描,使用IP地址为0.0.0.0,设置ACK位并在以太网层广播。    端口:1  服务:tcp…

    2022年7月15日
    19

发表回复

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

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