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过滤器指定过滤文件)

    过滤器的顺序由web.xml文件中&amp;lt;filter-mapping&amp;gt;的顺序决定,从上到下现有三个过滤器&amp;lt;filter&amp;gt;&amp;lt;filter-name&amp;gt;AFilter&amp;lt;/filter-name&amp;gt;&amp;lt;filter-class&amp;gt;com.jerry.filter.AF

    2022年4月12日
    151
  • 在bash中export命令作用是什么_bash:no such file or directory

    在bash中export命令作用是什么_bash:no such file or directoryexport  export命令将会使得被export的变量在运行的脚本(或shell)的所有的子进程中都可用.  不幸的是,没有办法将变量export到父进程(就是调用这个脚本或shell的进程)中.  关于export命令的一个重要的使用就是用在启动文件中,启动文件是用来初始化并且设置环境变量,让用户进程可以存取环境变量脚本不能export(导出)变量到它的父进程(p

    2022年9月3日
    2
  • 常微分方程初值问题数值解法MATLAB(泛函微分方程)

    Matlab解常微分方程的初值问题题目:Matlab解常微分方程的初值问题设计目的:1、熟练掌握Matlab的基本编程方法,及其编程风格。2、熟练掌握Matlab常用函数的使用。3、与本专业相关知识相结合,掌握其在程序开发中的应用方法以及和word、C语言等接口方法。4、通过计算机数值求解的方式来加深微分方程解的理解。5、熟悉初等方法可获得解析解之外的数值近似解的求解方法,提高对差分格式的认识…

    2022年4月12日
    187
  • 基于Linux的智能家居的设计(3)[通俗易懂]

    基于Linux的智能家居的设计(3)

    2022年2月6日
    35
  • #1032 : 最长回文子串

    #1032 : 最长回文子串#1032:最长回文子串时间限制:1000ms单点时限:1000ms内存限制:64MB描述   小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。   这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在

    2022年6月3日
    27
  • Java刷新bean重新加载bean 上下文 刷新bean

    Java刷新bean重新加载bean 上下文 刷新bean@Autowiredprivate ApplicationContext applicationContext;// 可以为接口或者业务方法被调用public void reloadInstance(){ //获取上下文 DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)applicationContext.getAutowireCapableBeanFactory();

    2022年8月19日
    14

发表回复

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

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