zoukankan      html  css  js  c++  java
  • UVAlive 6756 Increasing Shortest Path

    We all love short and direct problems, it is easier to write, read and understand the problem statement.
    Here is one of these problems. Life is too short to make a story", said Ahmed Aly.
    You are given a weighted directed graph of N nodes (the nodes are numbered from 1 to N), where
    the weights of the edges are distinct and positive. For each graph, you are also given a list of queries
    to answer.
    Each query will be represented by 3 integers A B C, which means you need to nd the shortest
    path (the path with minimum sum of weights of its edges) which goes from node A to node B and uses
    at most C edges, such that the weights of the edges in that path are in increasing order along the path,
    which means the weight of each edge in that path should be greater than the weight of the edge before
    it (unless it is the rst edge in the path).
    Your task is to write a program which answers these queries.
    Input
    Your program will be tested on one or more test cases. The rst line of the input will be a single
    integer T, the number of test cases (1  T  100). Followed by the test cases, the rst line of each
    test case contains 3 integers separated by a single space N M Q (2  N  150), (0  M  3; 000) and
    (1  Q  1; 000) representing the number of nodes, the number of edges and the number of queries,
    respectively. Followed by M lines, each line contains 3 integers separated by a single space X Y Z
    (1  X; Y  N) (1  Z  3; 000) which represent an edge going from the node X to the node Y with
    cost Z (X and Y will be different). Followed by Q lines, each line contains 3 integers separated by a
    single space A B C (1  A;B  N) (0  C  M) which represent a query as described above (A and
    B will be different).
    Note that there might multiple edges between the same pair of nodes.
    Output
    For each test case, print a single line for each query which contains a single integer, the minimum sum
    of weights for a path between the given pair of nodes which satis es the given constraints, or `-1' if
    there is no valid path between the given nodes which satis es the given constraints. The output must
    not contain empty lines between the cases.
    Sample Input
    1
    8 9 3
    1 2 1
    2 3 2
    3 4 3
    4 5 12
    5 8 7
    1 6 8
    6 4 9
    1 7 5
    7 4 4

    1 4 2
    1 4 3
    1 4 1


    Sample Output
    17
    6
    -1

    30秒 ,100个点,3000条边,1000次询问。

    从x到y用的边数不超过b条而且每次走过的边的权值都要递增所需要的权值和。

    先对边进行排序。

    然后用dp[i][j][k] 表示 i -> j 用的边数等于 k 条 所需要的最少权值。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL; 
    const int N=3010;
    const int M=151;
    const int inf = 1e9;
    
    struct node 
    {
        int x,y,w;
        bool operator < (const node &a)const{
            return w<a.w;
        }
    }e[N];
    
    int dp[M][M][M];
    int n,m,q;
    void update(int &a,int b){
        if(a==-1||b<a)a=b;
    }
    void run()
    {
        int x,y,b,w;
        scanf("%d%d%d",&n,&m,&q);
        memset(dp,-1,sizeof(dp));
        for(int i=0;i<=n;++i)
            dp[i][i][0]=0;
        
        for(int i=0;i<m;++i)
        {
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        }
        sort(e,e+m);
        for(int i=0;i<m;++i){
            for(int j=1;j<=n;++j){
                for(int k=1;k<n;++k){
                    if(    dp[j][e[i].x][ k-1 ] != -1){
                        update(dp[j][e[i].y][k],dp[j][e[i].x][k-1]+e[i].w);
                    }
                }
            }
        }
    
        for(int i=0;i<q;++i){
            scanf("%d%d%d",&x,&y,&b);
            if(b >= n) b=n-1;
            int ans = -1;
            for(int j=0;j<=b;++j){
                if( dp[x][y][j] != -1) update(ans,dp[x][y][j]);
            }
            printf("%d
    ",ans);
        }
    }
    int main()
    {
        int _;
        scanf("%d",&_);
        while(_--)run();
        return 0;
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    webpack介绍 安装 常用命令
    npm常用命令
    jQuery 实现点击页面其他地方隐藏菜单
    :after伪类+content内容生成经典应用举例
    几分钟前 几小时前 几天前... 7个阶段
    JS — 获取4个不重复的随机验证码
    JS — 数组去重(4种方法)
    Spring IOC原理简析
    浅谈Linux下的五种I/O模型
    大型分布式网站架构技术总结
  • 原文地址:https://www.cnblogs.com/hlmark/p/3928439.html
Copyright © 2011-2022 走看看