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 的解释:

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

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

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

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

    left shift count >= width of type

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

 #include <cstdio>
#include <queue>
#include <cstring>
#define NV 102
#define NE NV*NV using namespace std; //typedef __int64 LL;
typedef long long LL;
typedef LL Type;
const long long INF = 9223372036854775800LL; int nv, ne, tot;
Type dist[NV], cord[NV];
int eh[NV];
Type L[], D[];
bool vis[NV]; struct Edge{
int u, v, next;
Type cost;
Edge(){}
Edge(int a, Type c) : u(a), cost(c) {}
Edge(int a, int b, Type c, int d) : u(a), v(b), cost(c), next(d) {}
bool operator < (const Edge& x) const {
return cost > x.cost;
}
}edge[NE]; Type get_price(Type n)
{
for(int i = ; i < ; ++i)
if(L[i] < n && n <= L[i+]) return D[i];
return INF;
} void addedge(int a, int b, Type c)
{
Edge e = Edge(a, b, c, eh[a]);
edge[tot] = e;
eh[a] = tot++;
return;
} void init()
{
tot = ;
memset(vis, false, sizeof(vis));
memset(eh, -, sizeof(eh));
for(int i = ; i < nv; ++i)
for(int j = i+; j < nv; ++j)
{
Type road = cord[i] - cord[j];
if(road < ) road = -road;
Type price = get_price(road);
if(price != INF)
{
addedge(i, j, price);
addedge(j, i, price);
}
}
return;
} void SPFA(int s)
{
for(int i = ; i < nv; ++i) dist[i] = INF;
dist[s] = ;
priority_queue<Edge> que;
que.push(Edge(s, ));
vis[s] = true;
while(!que.empty())
{
Edge tmp = que.top();
que.pop();
int u = tmp.u;
vis[u] = false;
for(int i = eh[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if(dist[v] > edge[i].cost + dist[u])
{
dist[v] = edge[i].cost + dist[u];
if(!vis[v])
{
que.push(Edge(v, dist[v]));
vis[v] = true;
}
}
}
}
return;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("F:\\test\\input.txt", "r", stdin);
#endif
int T, m;
scanf("%d", &T);
for(int t = ; t <= T; ++t)
{
for(int i = ; i < ; ++i)
scanf("%I64d", &L[i]);
for(int i = ; i < ; ++i)
scanf("%I64d", &D[i]);
L[] = ;
scanf("%d%d", &nv, &ne);
for(int i = ; i < nv; ++i)
scanf("%I64d", &cord[i]);
init();
printf("Case %d:\n", t);
for(int i = , u, v; i != ne; ++i)
{
scanf("%d%d", &u, &v);
SPFA(u-);
if(dist[v-] == INF)
printf("Station %d and station %d are not attainable.\n", u, v);
else
printf("The minimum cost between station %d and station %d is %I64d.\n", u, v, dist[v-]);
}
}
return ;
}

HDU ACM 1690 Bus System (SPFA)的更多相关文章

  1. hdu 1690 Bus System (有点恶心)

    Problem Description Because of the huge population of China, public transportation is very important ...

  2. hdu 1690 Bus System(Dijkstra最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others ...

  3. 关于system(”pause“);的作用和意义

    注意:不要再return 的语句之后加,那样就执行不到了. system() 是调用系统命令:pause 暂停命令: 如果加有  system(”pause“): 这样在运行到此处时,会显示“Pres ...

  4. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  5. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  6. HDU.1233 还是畅通工程(Prim)

    HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...

  7. HDU 4370 0 or 1(spfa+思维建图+计算最小环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...

  8. hdu1690 Bus System(最短路 Dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  9. hdu 1690 Bus System (最短路径)

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. python对象属性管理(2):property管理属性

    使用Property管理属性 python提供了一种友好的getter.setter.deleter类方法的属性管理工具:property. property()是一个内置函数,它返回一个Proper ...

  2. Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站(一)--技术流ken

    前言 在之前已经写了关于Git,Gitlab以及Ansible的两篇博客<Git+Gitlab+Ansible剧本实现一键部署Nginx--技术流ken>,<Git+Gitlab+A ...

  3. Apollo 8 — ConfigService 异步轮询接口的实现

    源码 Apollo 长轮询的实现,是通过客户端轮询 /notifications/v2 接口实现的.具体代码在 com.ctrip.framework.apollo.configservice.con ...

  4. 利用Python测量滴水湖的水面面积

    美丽的滴水湖   美丽的滴水湖坐落在上海的东南角,濒临东海,风景秀丽,安静舒适,是旅游.恋爱的绝佳去处.笔者有幸去过一回,对那儿的风土人情留下了深刻的印象,如果有机会,笔者还会多去几次!   滴水湖是 ...

  5. 第一册:lesson seventy three.

    原文: The way to King Street. Last week Mrs. Mills went to London. She does not know London very well, ...

  6. 【转载】 IIS服务器防盗链设置

    在实际运行的服务器环境中,我们自己网站中的资源一般不希望被外部网站引用,被外部网站引用IIS网站中的资源文件,一是会加重了服务器的负担,二是占用了你自己服务器的外网带宽资源,因此我们希望防止盗链这种情 ...

  7. 深入理解JAVA中的NIO

    前言: 传统的 IO 流还是有很多缺陷的,尤其它的阻塞性加上磁盘读写本来就慢,会导致 CPU 使用效率大大降低. 所以,jdk 1.4 发布了 NIO 包,NIO 的文件读写设计颠覆了传统 IO 的设 ...

  8. JAVA面试题(8)

    java基础以及多个“比较” 1.Collections.sort排序内部原理 在Java 6中Arrays.sort()和Collections.sort()使用的是MergeSort,而在Java ...

  9. Reinforcement Learning: An Introduction读书笔记(3)--finite MDPs

     > 目  录 <  Agent–Environment Interface Goals and Rewards Returns and Episodes Policies and Val ...

  10. React之todo-list

    基于React的一个简单Todo-list 先赌为快:在线DEMO,感觉还不错点一下star  -_- ~ 源码地址: 一.已经完成的功能 1.新增选项(默认未完成) 2.完成状态可以切换 3.当前选 ...