• Heavy Transportation(最短路 + dp)


    Heavy Transportation
    Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

    Description

    Background 
    Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
    Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

    Problem 
    You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

    Input

    The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

    Output

    The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
     
    不用邻接表会超时。注意输出格式。
     
    AC Code:
     1 /**
     2 *Dijkstra + 静态邻接表 + 优先队列优化
     3 */
     4 
     5 #include <iostream>
     6 #include <deque>
     7 #include <queue>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <algorithm>
    11 
    12 using namespace std;
    13 
    14 const int MAXV = 1001;  //最大边数
    15 const int INF = 0x3f3f3f3f;  //最大权值
    16 struct Edge
    17 {
    18     int to;  
    19     int link;  
    20     int w;  
    21     void set_val(int a, int b, int c){to = a, link = b, w = c;}
    22 }edge[MAXV * MAXV >> 1];  //存储边
    23 int pre[MAXV];
    24 
    25 struct Node
    26 {
    27     int v;  //顶点的标号
    28     int w;  //顶点v到源点的最短路
    29     Node(int a, int b) {v = a; w = b;}
    30     void set_val(int a, int b) {v = a; w = b;}
    31 };  //设立该结构体的目的:作为优先队列的结点
    32 int d[MAXV];  //记录最短路
    33 bool done[MAXV];  //记录是否已找到最短路,避免重复访问
    34 int n, m;
    35 
    36 bool operator < (const Node& x, const Node& y)
    37 {
    38     return x.w < y.w;
    39 }
    40 
    41 int main()
    42 {
    43     int t, ca = 1;
    44     scanf("%d", &t);
    45     while(t--){
    46         scanf("%d %d", &n, &m);
    47         //建立静态邻接表
    48         memset(pre, -1, sizeof(pre));
    49         for(int i = 0; m--; ){
    50             int a, b, c;
    51             scanf("%d %d %d", &a, &b, &c);
    52             edge[i].set_val(a, pre[b], c);
    53             pre[b] = i++;
    54             edge[i].set_val(b, pre[a], c);
    55             pre[a] = i++;
    56         }
    57 
    58         //执行Dij算法,使用最小堆进行优化
    59         memset(done, false, sizeof(done));
    60         memset(d, 0, sizeof(d));  //d数组的初始化方式是关键!
    61         d[1] = INF;
    62         priority_queue<Node> que;
    63         que.push(Node(1, d[1]));  //源点入队
    64         done[1] = true;
    65         while(!que.empty()){
    66             Node cur = que.top();
    67             que.pop();
    68             for(int i = pre[cur.v]; i != -1; i = edge[i].link){
    69                 int to = edge[i].to;
    70                 if(!done[to] && d[to] < min(cur.w, edge[i].w)){
    71                     d[to] = min(cur.w, edge[i].w);
    72                     que.push(Node(to, d[to]));
    73                 }
    74             }
    75         }
    76 
    77         //输出结果
    78         printf("Scenario #%d:
    %d
    
    ", ca++, d[n]);
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    学习 Linux 几点忠告【转载】
    游侠更新仙剑全系列免CD补丁(支持WIN7 SP1)【转载】
    更改数据库对象所有者
    数据库 行列相互转化
    JQuery计时器
    js操作cookies
    利用自定义DataTable来重画数据集的用法
    asp.net mvc 从客户端中检测到有潜在危险的 Request.Form 值的解决方法
    CS144 Lab
    CS231n Assignment #2
  • 原文地址:https://www.cnblogs.com/cszlg/p/3318282.html
Copyright © 2020-2023  润新知