zoukankan      html  css  js  c++  java
  • HDU ACM 1690 Bus System (SPFA)


    Bus System

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

    【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=1690

    【解题思路】SPFA求最短路径问题,将每个站之间的距离转化为相应的价格从而建立两站点相连的边,其中如果距离超出了题目给的价表,那么就说明这两点不连接,连接的边的权值为价表相应区间的价格,这题数据有点大,需要用long long,不过我在用long long的时候出现两个问题

    问题1:关于long long 报错的问题,本地编译器没问题,用G++提交后出错:

    在用long long 类型声明和定义一个变量时,明显地在其表示范围内赋值,却得到如下的错误:

    error: integer constant is too large for "long" type

    来自 http://china.xilinx.com/support/answers/31999.html 的解释:

     1 /*Description
     2  *When I define a long long integer data type in SW application in EDK, a warning / error similar to the following occurs:
     3  *"warning: integer constant is too large for 'long' type".
     4  *Example:
     5  */
     6  
     7 int main ()
     8 {
     9 long long int test = 0x0008888000000000;
    10 }
    11 
    12 /*SOLUTION
    13  *The warning message can be safely ignored, as mb-gcc is not doing anything wrong; the 64-bit computing is in fact correct.
    14  *This warning occurs because gcc is strict in syntax and requires LL on the end of such constants. 
    15  *This warning message disappears if the integer is appended with LL.
    16  */
    17  
    18 long long int test = 0x0008888000000000LL;

    后来在网上搜到这样的一个解释(http://hi.baidu.com/zealot886/item/301642b0e98570a9ebba932f):

     1 /*PS: 英语是硬伤,还是没搞懂原因,字面上的意思是说C不够聪明去判断左边的类型,类型仅仅是文本上的属性,不是我们所看到的的语境? 
     2  *
     3  *The letters 100000000000 make up a literal integer constant, but the value is too large for the type int.
     4  *You need to use a suffix to change the type of the literal, i.e.
     5  *
     6  *    long long num3 =100000000000LL;
     7  *
     8  *The suffix LL makes the literal into type long long.  
     9  *C is not "smart" enough to conclude this from the type on the left,
    10  *the type is a property of the literal itself, not the context in which it is being us.
    11  */

    问题2:在用位运算给long long 类型赋值的时候,出现下面的Warning:

        left shift count >= width of type

    上网找了下,想到应该跟问题1应该有点联系,1 在这里是int型,需要进行显式转换才能进行左移,明显地溢出

      1 #include <cstdio>
      2 #include <queue>
      3 #include <cstring>
      4 #define NV 102
      5 #define NE NV*NV
      6 
      7 using namespace std;
      8 
      9 //typedef __int64 LL;
     10 typedef long long LL;
     11 typedef LL Type;
     12 const long long INF = 9223372036854775800LL;
     13 
     14 int nv, ne, tot;
     15 Type dist[NV], cord[NV];
     16 int eh[NV];
     17 Type L[5], D[5];
     18 bool vis[NV];
     19 
     20 struct Edge{
     21     int u, v, next;
     22     Type cost;
     23     Edge(){}
     24     Edge(int a, Type c) : u(a), cost(c) {}
     25     Edge(int a, int b, Type c, int d) : u(a), v(b), cost(c), next(d) {}
     26     bool operator < (const Edge& x) const {
     27         return cost > x.cost;
     28     }
     29 }edge[NE];
     30 
     31 Type get_price(Type n)
     32 {
     33     for(int i = 0; i < 4; ++i)
     34         if(L[i] < n && n <= L[i+1]) return D[i];
     35     return INF;
     36 }
     37 
     38 void addedge(int a, int b, Type c)
     39 {
     40     Edge e = Edge(a, b, c, eh[a]);
     41     edge[tot] = e;
     42     eh[a] = tot++;
     43     return;
     44 }
     45 
     46 void init()
     47 {
     48     tot = 0;
     49     memset(vis, false, sizeof(vis));
     50     memset(eh, -1, sizeof(eh));
     51     for(int i = 0; i < nv; ++i)
     52     for(int j = i+1; j < nv; ++j)
     53     {
     54         Type road = cord[i] - cord[j];
     55         if(road < 0) road = -road;
     56         Type price = get_price(road);
     57         if(price != INF)
     58         {
     59             addedge(i, j, price);
     60             addedge(j, i, price);
     61         }
     62     }
     63     return;
     64 }
     65 
     66 
     67 void SPFA(int s)
     68 {
     69     for(int i = 0; i < nv; ++i) dist[i] = INF;
     70     dist[s] = 0;
     71     priority_queue<Edge> que;
     72     que.push(Edge(s, 0));
     73     vis[s] = true;
     74     while(!que.empty())
     75     {
     76         Edge tmp = que.top();
     77         que.pop();
     78         int u = tmp.u;
     79         vis[u] = false;
     80         for(int i = eh[u]; i != -1; i = edge[i].next)
     81         {
     82             int v = edge[i].v;
     83             if(dist[v] > edge[i].cost + dist[u])
     84             {
     85                 dist[v] = edge[i].cost + dist[u];
     86                 if(!vis[v])
     87                 {
     88                     que.push(Edge(v, dist[v]));
     89                     vis[v] = true;
     90                 }
     91             }
     92         }
     93     }
     94     return;
     95 }
     96 
     97 
     98 int main()
     99 {
    100     #ifndef ONLINE_JUDGE
    101     freopen("F:\test\input.txt", "r", stdin);
    102     #endif
    103     int T, m;
    104     scanf("%d", &T);
    105     for(int t = 1; t <= T; ++t)
    106     {
    107         for(int i = 1; i < 5; ++i)
    108             scanf("%I64d", &L[i]);
    109         for(int i = 0; i < 4; ++i)
    110             scanf("%I64d", &D[i]);
    111         L[0] = 0;
    112         scanf("%d%d", &nv, &ne);
    113         for(int i = 0; i < nv; ++i)
    114             scanf("%I64d", &cord[i]);
    115         init();
    116         printf("Case %d:
    ", t);
    117         for(int i = 0, u, v; i != ne; ++i)
    118         {
    119             scanf("%d%d", &u, &v);
    120             SPFA(u-1);
    121             if(dist[v-1] == INF)
    122                 printf("Station %d and station %d are not attainable.
    ", u, v);
    123             else
    124                 printf("The minimum cost between station %d and station %d is %I64d.
    ", u, v, dist[v-1]);
    125         }
    126     }
    127     return 0;
    128 }
  • 相关阅读:
    Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
    Codeforces Round #500 (Div. 2) D
    AtCoder Grand Contest 026 D
    Codeforces Round #495 (Div. 2) Sonya and Matrix
    AtCoder Regular Contest 100 E
    1013 数素数
    1010 一元多项式求导(用while接收输入)
    1009 说反话(字符串、栈)
    L2-006 树的遍历 (后序中序求层序)
    L2-004 这是二叉搜索树吗?
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3231308.html
Copyright © 2011-2022 走看看