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. 通过修改hosts解决gist.github.com无法访问的问题

    1.打开mac终端先ping一下 ping 192.30.253.119 如果能ping通的话 ,说明可以访问 2.修改hosts文件,添加如下语句: 192.30.253.118 gist.gith ...

  2. Perl文件句柄相关常量变量

    文件句柄相关变量 对应的官方手册:http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles 默认情况下: $/:输入行 ...

  3. Raft 基础

    目录 三个状态 什么是任期 节点之间的通信 1. 三个状态 Raft 设计了 3 个状态,用于表示节点的状态,分别是跟随者,候选者,领导者. 领导者:通常只有一个领导人,并且其他节点都是跟随者. 跟随 ...

  4. IntelliJ IDEA为类和方法自动添加注释

    1.为类添加自动注释模版 File-->Settings-->Editor-->File and Code Templates /** * @author :mmzs * @date ...

  5. Idea 常用功能汇总,工作中常用技巧,移出请说明原因,笔记花了好长时间汇总的

    1.隐藏没用到的文件 比如 IDEA 的项目配置文件(.iml 和.idea),打开 Settings-File Types, 加入要隐藏的文件后缀.  2.常用技巧 2.1 通过Alt+F8查看变量 ...

  6. Linux-bg和fg命令(19)

    使用ctrl+z将程序挂在后台: jobs 查看后台的命令: fg(fore go) 将后台的命令,放置前台(fore)继续执行,比如:fg 2     //等价于vi 2.txt bg(back g ...

  7. 史上最全python面试题详解(三)(附带详细答案(关注、持续更新))

    38.面向对象深度优先和广度优先是什么? 39.面向对象中super的作用? 40.是否使用过functools中的函数?其作用是什么? Python自带的 functools 模块提供了一些常用的高 ...

  8. JDK的动态代理-----为接口进行代理

    JDK的动态代理是必须掌握的,动态代理的好处就不用我多说了吧 :) 小弟最近在研究mybatis的源码实现,就开始了解mybatis的Mapper代理机制,为什么接口不用实现类也能代理? 好了,废话不 ...

  9. 初识 Java-监听器

    使用Listener类当java  web应用程序在web容器中运行时,在java web应用程序内部会不断发生各种事件,例如web应用的启动,暂停,销毁等.以及web应用中session开始和结束 ...

  10. C#两个时间相减

    原文地址:http://www.jb51.net/article/60177.htm using System; using System.Collections.Generic; using Sys ...