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. mcrypt加密以及解密过程

    Mcrypt库支持20多种加密算法和8种加密模式,具体可以通过函数mcrypt_list_algorithms()和mcrypt_list_modes()来显示 Mcrypt扩展库可以实现加密解密功能 ...

  2. 【SqlServer系列】数据库三大范式

    1   概述 一般地,在进行数据库设计时,应遵循三大原则,也就是我们通常说的三大范式,即第一范式要求确保表中每列的原子性,也就是不可拆分:第二范式要求确保表中每列与主键相关,而不能只与主键的某部分相关 ...

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

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

  4. 第一册:lesson seventy nine.

    原文: Carol's shopping list. What are you doing Carol? I'm making a shopping list Tom. What do we need ...

  5. 在Azure虚拟机上部署FileZilla FTP服务器

    1.开始之前准备的软件 ①一台Azure虚拟机 ②FileZilla服务端安装包 我这边是windows的所以 给个链接 https://filezilla-project.org/download. ...

  6. SET XACT_ABORT ON [SQL SERVER] 设置事务全部回滚

    SET XACT_ABORT ON 设置事务回滚的默认是OFF. 当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚错误的地方

  7. [PHP] swoole的安装和简单使用

    1. pecl install swoole 开启http2支持需要的依赖库:apt-get install nghttp2 开启的几个参数: enable sockets supports? [no ...

  8. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  9. NPM测试模块之rewire教程

    摘要:有了rewire模块,再也不用担心测试私有函数了. 在玩转Node.js单元测试,我介绍了3个用于编写测试代码的NPM模块:Mocha, Should以及SuperTest.为了怂恿大家写单元测 ...

  10. 协程与Epoll的配合

    想快速了解协程与网络调用的原来么,那么请赶紧关闭本页,因为下面都是在扯淡. 这几天是端午假期,第一天大算照着网上一大堆基于ucontext来写协程的文章自己也写一个简单的协程实现.于是第一天我就开始动 ...