zoukankan      html  css  js  c++  java
  • HDU 1874 畅通工程续 2008浙大研究生复试热身赛(2)

    畅通工程续
    Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 79501 Accepted Submission(s): 30582

    Problem Description
    某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

    现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

    Input
    本题目包含多组数据,请处理到文件结束。
    每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
    接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
    再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

    Output
    对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

    Sample Input
    3 3
    0 1 1
    0 2 3
    1 2 1
    0 2
    3 1
    0 1 1
    1 2

    Sample Output
    2
    -1

    Author
    linle

    Source
    2008浙大研究生复试热身赛(2)——全真模拟

    Recommend
    lcy

    这个题是个最短路的的方法都能求

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int dis[1050][1050];
    int main()
    {
        int n,m,x,y,d,max1;
        while(~scanf("%d %d",&n,&m)&&n&&m)
        {
            memset(dis,0x3f,sizeof(dis));
            max1=0;
            for(int i=0;i<m;i++)
            {
                cin>>x>>y>>d;
                dis[x][y]=min(d,dis[x][y]);
                dis[y][x]=dis[x][y];
            }
            for(int k=0;k<=n-1;k++)
                for(int i=0;i<=n-1;i++)
                    for(int j=0;j<=n-1;j++)
                    {
                        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                    }
            cin>>x>>y;
            if(x>y) swap(x,y);
            else if(x==y) {
                cout<<0<<endl;
                continue;
            }
            if(dis[x][y]>=0x3f3f3f3f)cout<<-1<<endl;
            else cout<<dis[x][y]<<endl;
        }
    }
    
  • 相关阅读:
    测试是否有必要看开发代码?如何能看懂?
    【LeetCode】111. 二叉树的最小深度(BFS 解题套路框架,要会默写)
    【LeetCode】112. 路径总和
    【测试开发】知识点配置 Nginx 解决多端口访问
    【测试开发】知识点使用EasyExcel,实现excel导出和导入
    p5 随机圆连接背景和代码树
    angular技巧
    javascript原生技巧篇
    MybatisPlus
    安装 jupyter notebook
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798888.html
Copyright © 2011-2022 走看看