zoukankan      html  css  js  c++  java
  • 畅通工程续

    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
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 #include<vector>
     7 const int maxn = 205;
     8 vector <pair<int, int> > E[maxn];
     9 int dis[maxn];
    10 int n, m,s,t;
    11 void init()
    12 {
    13     for (int i = 0; i < maxn; i++)
    14         E[i].clear(), dis[i] = 1e9;
    15 }
    16 void dit()
    17 {
    18     dis[s] = 0;
    19     priority_queue<pair<int, int> > q;
    20     q.push(make_pair(-dis[s], s));
    21     while (!q.empty())
    22     {
    23         int te = q.top().second;
    24         q.pop();
    25         for (int i = 0; i < E[te].size(); i++)
    26         {
    27             int d = E[te][i].first;
    28             int k = E[te][i].second;
    29             if (dis[d] > dis[te] + k)
    30             {
    31                 dis[d] = dis[te] + k;
    32                 q.push(make_pair(-dis[d], d));
    33             }
    34         }
    35     }
    36     if (dis[t] == 1e9)
    37         cout << "-1" << endl;
    38     else cout << dis[t] << endl;
    39 }
    40 int main()
    41 {
    42     while (cin >> n >> m)
    43     {
    44         init();
    45         for (int i = 0; i < m; i++)
    46         {
    47             int t1, t2, t3;
    48             scanf("%d %d %d", &t1, &t2, &t3);
    49             E[t1].push_back(make_pair(t2, t3));
    50             E[t2].push_back(make_pair(t1, t3));
    51         }
    52         scanf("%d %d", &s, &t);
    53         dit();
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    cmd开启3389,无需重启!
    x86的控制寄存器CR0,CR1,CR2,CR3
    x64下fs的角色已经换成了gs
    在win64里,只有一种调用约定
    fs寄存器
    【转】C++ 编译器的函数名修饰规则
    windbg ida需要symbols
    WIN7-X64内核模式下编程实现导出表列表查看
    VS2010+WDK配置要点
    比特币 —— 学习笔记(一)
  • 原文地址:https://www.cnblogs.com/kangdong/p/9185114.html
Copyright © 2011-2022 走看看