zoukankan      html  css  js  c++  java
  • HDU 2783 You’ll be Working on the Railroad(最短路)

    You’ll be Working on the Railroad

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 246    Accepted Submission(s): 63


    Problem Description
    Congratulations! Your county has just won a state grant to install a rail system between the two largest towns in the county -- Acmar and Ibmar. This rail system will be installed in sections, each section connecting two different towns in the county, with the first section starting at Acmar and the last ending at Ibmar. The provisions of the grant specify that the state will pay for the two largest sections of the rail system, and the county will pay for the rest (if the rail system consists of only two sections, the state will pay for just the larger section; if the rail system consists of only one section, the state will pay nothing). The state is no fool and will only consider simple paths; that is, paths where you visit a town no more than once. It is your job, as a recently elected county manager, to determine how to build the rail system so that the county pays as little as possible. You have at your disposal estimates for the cost of connecting various pairs of cities in the county, but you're short one very important requirement -- the brains to solve this problem. Fortunately, the lackeys in the computing services division will come up with something.
     
    Input
    Input will contain multiple test cases. Each case will start with a line containing a single positive integer n<=50 , indicating the number of railway section estimates. (There may not be estimates for tracks between all pairs of towns.) Following this will be n lines each containing one estimate. Each estimate will consist of three integers s e c , where s and e are the starting and ending towns and c is the cost estimate between them. (Acmar will always be town 0 and Ibmar will always be town 1. The remaining towns will be numbered using consecutive numbers.) The costs will be symmetric, i.e., the cost to build a railway section from town s to town e is the same as the cost to go from town e to town s , and costs will always be positive and no greater than 1000. It will always be possible to somehow travel from Acmar to Ibmar by rail using these sections. A value of n = 0 will signal the end of input.
     
    Output
    For each test case, output a single line of the form

    c1 c2 ... cm cost

    where each ci is a city on the cheapest path and cost is the cost to the county (note c1 will always be 0 and cm will always be 1 and ci and ci + 1 are connected on the path). In case of a tie, print the path with the shortest number of sections; if there is still a tie, pick the path that comes first lexicographically.
     
    Sample Input
    7
    0 2 10
    0 3 6
    2 4 5
    3 4 3
    3 5 4
    4 1 7
    5 1 8
    0
     
    Sample Output
    0 3 4 1 3

    题意是给出 n 条边。

    问从 0 -> 1 可以忽略2条路权,所需总权最小是多少,路径是什么 。

    只有1条边时,不可忽略。

    有2条时,可以忽略一条。

    因为 n 是很少 , 直接暴力枚举可以忽略的边。

    然后跑dij.来维护一个最优的路径。

    这条题思路不难,不过就是写起来有点恶心

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <iostream>
    #include <map>
    #include <stack>
    using namespace std ;
    typedef pair<int,int> pii ;
    #define X first
    #define Y second
    const int N = 1010 ;
    const int inf = 1e9+7 ;
    typedef long long LL;
    int n , xx[N] , pre[N] , ww[N] , tot ;
    
    vector<pii>g[N];
    
    
    struct node {
        int a , c , d , id ;
        node(){};
        node( int a , int c , int d , int id ):a(a),c(c),d(d),id(id){}
        bool operator < ( const node &A ) const {
            if( d != A.d ) return d > A.d ;
            else if( c != A.c ) return c > A.c ;
            else {
                stack<int>s1 , s2; int id1 = id , id2 = A.id ;
                while( id1 != -1 ) { s1.push( xx[id1] ); id1 = pre[id1] ; }
                while( id2 != -1 ) { s2.push( xx[id2] ); id2 = pre[id2] ; }
                while( !s1.empty() ) {
                    int a = s1.top() ; s1.pop() ;
                    int b = s2.top() ; s2.pop() ;
                    if( a > b ) return true ;
                }
                return false ;
            }
        }
    };
    int bestpath[N] , bestcnt , bestcost ;
    int tmppath[N] , tmpcnt , tmpcost ;
    
    
    void Choose_best( int id , int cnt ) {
        if( tmpcost > bestcost ) return  ;
        stack<int>s ;
        while( id != -1 ) { s.push( xx[id] ); id = pre[id] ; }
        tmpcnt = 0 ;
        while( !s.empty() ) { tmppath[tmpcnt++] = s.top() ; s.pop() ; }
    
        if( tmpcost < bestcost ) {
            bestcnt = tmpcnt ;
            bestcost = tmpcost ;
            for( int i = 0 ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
            return ;
        }
        if( cnt > bestcnt ) return ;
        else if( cnt < bestcnt ) {
            bestcnt = tmpcnt ;
            for( int i = 0 ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
        } else {
            for( int i = 0 ; i < tmpcnt ; ++i ) {
                if( bestpath[i] > tmppath[i] ) {
                    for( int j = i ; j < bestcnt ; ++j ) {
                        bestpath[j] = tmppath[j] ;
                    }
                } else if ( bestpath[i] < tmppath[i] ) {
                    break ;
                }
            }
        }
    }
    
    int dis[N] ;
    
    void dij() {
        memset( dis , 0x3f , sizeof dis );
        priority_queue<node>que;
        tot = 0 ; xx[tot] = 0 , pre[tot] = -1 ; tot++ ;
        que.push( node(0,0,0,tot-1) );
        dis[0] = 0 ;
        while( !que.empty() ) {
            int u = que.top().a , cnt = que.top().c , cost = que.top().d , id = que.top().id ;
            que.pop();
            if( cost > dis[u] ) continue ;
            if( u == 1 ) {
                if( dis[u] ) {
                    tmpcost = dis[u] ;
                    Choose_best( id , cnt );
                }
                return ;
            }
            for( int i = 0 ; i < g[u].size() ; ++i ) {
                int v = g[u][i].X , w = ww[g[u][i].Y] ;
                if( dis[v] > dis[u] + w ) {
                    dis[v] = dis[u] + w ;
                    xx[tot] = v ; pre[tot] = id ; tot++;
                    que.push( node( v , cnt+1 , dis[v] , tot - 1 ) ) ;
                }
            }
        }
    }
    
    void Gao() {
        dij();
        for( int i = 0 ; i < n ; ++i ) {
            int cc = ww[i] ; ww[i] = 0 ;
            dij();
            for( int j = i + 1 ; j < n ; ++j ) {
                int dd = ww[j] ; ww[j] = 0 ;
                dij(); ww[j] = dd ;
            }
            ww[i] = cc ;
        }
        for( int i = 0 ; i < bestcnt ; ++i ) cout << bestpath[i] << ' ' ;
        cout << bestcost << endl ;
    }
    
    int Run() {
        while( cin >> n && n ) {
            bestcost = bestcnt = inf ;
            for( int i = 0 ; i < N ; ++i ) g[i].clear();
            for( int i = 0 ; i < n ; ++i ) {
                int u , v , w ; cin >> u >> v >> w ;
                ww[i] = w ;
                g[u].push_back(pii(v,i));
                g[v].push_back(pii(u,i));
            }
            Gao();
        }
        return 0 ;
    }
    
    int main() {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(0);
        return Run();
    }
    View Code
  • 相关阅读:
    java学习day39--SSM整合(方案二)
    java学习day39--SSM整合(方案一)
    @PathVariable注解的作用
    Ajax接收Json数据,调用template模板循环渲染页面的方法
    JS中的DOM与BOM
    关于req.params、req.query、req.body等请求对象
    EJS 高效的 JavaScript 模板引擎
    JavaScript中的变量在内存中的具体存储形式
    JavaScript规定了几种语言类型?
    移动端电商项目总结
  • 原文地址:https://www.cnblogs.com/hlmark/p/4395271.html
Copyright © 2011-2022 走看看