zoukankan      html  css  js  c++  java
  • HDU Power transmission(最短路)

    嗯,不得不承认自己做题真的太不用心了,不去想实现的细节,总是错在细节,不能怨别人不相信自己,是自己不够让别人相信,代码能力差是一回事,但用不用心是另一回事~好好检讨一下自己。

    题意:给出N个电力传输点,电力每经过一个点会损耗一定电力,让你找出一条损耗电力最小的路;

    思路:题目中的数据范围很大,要用邻接表存储边,用队列搜索比较快。

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <math.h>
    #define N 50005
    #define M 2500005
    #define INF 100000000
    using namespace std ;
    
    struct point
    {
        int end ;
        int cost ;
    }p[M] ;
    
    int node[N] , next[M] ;
    int vist[N] ;
    int q[N] , head , tail ;
    double dis[N] ;
    int n , k , cnt ;
    
    void add( int x , int y , int z )
    {
        p[cnt].end = y ;
        p[cnt].cost = z ;
        next[cnt] = node[x] ;
        node[x] = cnt++ ;
    }
    
    void spfa ( int s , int e , int sum )
    {
        int u , v ;
        for ( u = 0 ; u <= n ; u++ )
        {
            dis[u] = -1.0 ;
            vist[u] = 0 ;
        }
        dis[s] = sum ;
        vist[s] = 1 ;
        head = tail = 0 ;
        q[head++] = s ;
        while ( head != tail )
        {
            u = q[tail++] ;
            vist[u] = 0 ;
            v = node[u] ;
            while ( v != -1 )
            {
                if ( dis[u] * (( 100.0 - p[v].cost ) / 100.0 ) > dis[p[v].end] )
                {
                    dis[p[v].end] = dis[u] * (( 100.0 - p[v].cost ) / 100.0 ) ;
                    if ( !vist[p[v].end] )
                    {
                        vist[p[v].end] = 1 ;
                        q[head++] = p[v].end ;
                        if ( head == N ) head = 0 ;
                    }
                }
                v = next[v] ;
            }
            if ( tail == N ) tail = 0 ;
        }
    }
    
    int main()
    {
        int i , j , k , x , z ;
        int s , e , sum ;
    
        while ( scanf ( "%d" , &n) != EOF )
        {
            memset( node , -1 , sizeof ( node ));
            cnt = 0 ;
            for ( i = 1; i <= n ; i++ )
            {
                //cin>>k ;
                scanf ( "%d" , &k );
                for( j = 1 ; j <= k ; j++ )
                {
                    //cin>>x>>z ;
                    scanf ( "%d%d" , &x , &z );
                    add( i , x , z );
                }
            }
            //cin>>s>>e>>sum;
            scanf( "%d%d%d" ,&s , &e , &sum );
            spfa( s , e , sum );
            printf ( "%.2lf\n" , sum - dis[e] );
        }
        return 0 ;
    }
  • 相关阅读:
    matlab 绘制条状图形
    细思恐极 天价房都被谁买去了?——如何操作?
    matlab中的containers.Map()
    林彪:怎样当好一个师长?
    matlab 怎么建立结构体数组?
    matlab中patch函数的用法
    Ubuntu 安装配置MySQL,并使用VS的Server Explorer UI界面远程管理MySQL
    CLIQUE 聚类算法以及Java实现+多线程
    R 中同步进行的多组比较的包:npmc
    基于D3JS绘制中国地图
  • 原文地址:https://www.cnblogs.com/misty1/p/2611170.html
Copyright © 2011-2022 走看看