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
php

【題目連接】http://acm.hdu.edu.cn/showproblem.php?pid=1690html

【解題思路】SPFA求最短路徑問題,將每一個站之間的距離轉化爲相應的價格從而創建兩站點相連的邊,其中若是距離超出了題目給的價表,那麼就說明這兩點不鏈接,鏈接的邊的權值爲價表相應區間的價格,這題數據有點大,須要用long long,不過我在用long long的時候出現兩個問題app

問題1:關於long long 報錯的問題,本地編譯器沒問題,用G++提交後出錯:ui

在用long long 類型聲明和定義一個變量時,明顯地在其表示範圍內賦值,卻獲得以下的錯誤:this

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

來自 http://china.xilinx.com/support/answers/31999.html 的解釋:spa

 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):code

 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:htm

    left shift count >= width of typeblog

上網找了下,想到應該跟問題1應該有點聯繫,1 在這裏是int型,須要進行顯式轉換才能進行左移,明顯地溢出ip

 

  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:\n", 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.\n", u, v);
123             else
124                 printf("The minimum cost between station %d and station %d is %I64d.\n", u, v, dist[v-1]);
125         }
126     }
127     return 0;
128 }
相關文章
相關標籤/搜索