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 的解释:
/*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)的更多相关文章
- hdu 1690 Bus System (有点恶心)
Problem Description Because of the huge population of China, public transportation is very important ...
- hdu 1690 Bus System(Dijkstra最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others ...
- 关于system(”pause“);的作用和意义
注意:不要再return 的语句之后加,那样就执行不到了. system() 是调用系统命令:pause 暂停命令: 如果加有 system(”pause“): 这样在运行到此处时,会显示“Pres ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- HDU 4370 0 or 1(spfa+思维建图+计算最小环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...
- hdu1690 Bus System(最短路 Dijkstra)
Problem Description Because of the huge population of China, public transportation is very important ...
- hdu 1690 Bus System (最短路径)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- FineUI开源版(ASP.Net)初学手册-部分JS整理
有人老找JS,我吧FineUI自己写的JS沾过来方便大家看看,在实现前端的时候更灵活 JS 实例 注释 控件 F.ready F.ready(function(){}); 就是ready 很多方法 ...
- MySQL系列详解二:MySQL语句操作-技术流ken
简介 本篇博客将详细讲解mysql的一些常用sql语句操作,例如创建数据库,删除数据库,创建表,修改表,删除表,以及简单查询案例. 关于mysql数据中的SQL的大小写问题 1.不区分大小写 1. s ...
- maven web工程缺少 src/main/java 和 src/test/java 资源文件夹的方法
右键打开:build path -> configure build path... 在弹出的界面,选择: 编辑后: 点击finish,即可完成
- T-SQL基础(二)之关联查询
在上篇博文中介绍了T-SQL查询的基础知识,本篇主要介绍稍微复杂的查询形式. 表运算符 表运算符的作用是把为其提供的表作为输入,经过逻辑查询处理,返回一个表结果.SQL Server支持四个表运算符: ...
- 原创SQlServer数据库生成简单的说明文档包含(存储过程、视图、数据库批量备份)小工具(附源码)
这是一款简单的数据库文档生成工具,主要实现了SQlServer生成说明文档的小工具,目前不够完善,主要可以把数据库的表以及表的详细字段信息,导出到 Word中,可以方便开发人员了解数据库的信息或写技术 ...
- mysql 的show processlist和show full processlist区别
processlist命令的输出结果显示了有哪些线程在运行,不仅可以查看当前所有的连接数,还可以查看当前的连接状态帮助识别出有问题的查询语句等. 如果是root帐号,能看到所有用户的当前连接.如果是其 ...
- Hibernate入门(三)
持久化类的编写规则: 1.持久化类需要提供无参的构造方法.因为在Hibernate的底层需要使用反射生成的实例. 2.持久化类的属性需要私有,对私有的属性提供共有的get和set方法.因为在Hiber ...
- TCP连接与释放
TCP连接的建立 三次握手 TCP服务器进程先创建传输控制块TCB,时刻准备接受客户进程的连接请求,此时服务器就进入了LISTEN(监听)状态. TCP客户进程也是先创建传输控制块TCB,然后向服务器 ...
- html5基础!!
A:HTML5 不基于 SGML,所以不需要引用 DTD: B: HTML 4.01 基于 SGML,而HTML5不基于SGML: DTD可定义合法的XML文档构建模块,它使用一系列合法的元素来定义文 ...
- CSS-水平和垂直居中
div中水平和垂直居中 width:200px;height:200px; /*设置div的大小*/ border:1px solid green; /*边框*/ text-align: center ...