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. WPF,强制捕获鼠标事件,鼠标移出控件外依然可以执行强制捕获的鼠标事件

    在WPF中,只有鼠标位置在某个控件上的时候才会触发该控件的鼠标事件.例如,有两个控件都注册了MouseDown和MouseUp事件,在控件1上按下鼠标,不要放开,移动到控件2上再放开.在这个过程中,控 ...

  2. Windows 10 安装 Mongod

    因为新换了Windows 10 电脑,需要在新电脑重新安装所有的软件,包括mongodb 下载文件:首先在mongodb的官方网站上下载最新版本的mongodb安装程序,https://www.mon ...

  3. 学习记录---C# Web程序获取客户端电脑信息

    问题描述:由于最近项目需要使用Mac地址与注册码进行加密处理,但是又因为Web程序的局限性不能获取客户端电脑系统信息,当然IE浏览器有一个activex控件他是可以通过Js在前端代码中直接获取的,局限 ...

  4. MATLAB R2017a 进入主界面以后一直处于初始化状态的解决办法

    自从前几天更新了win10系统,结果发现matlab不能用了,进入主界面一直初始化,没完没了. 网上说可能是许可证等问题,但经过尝试发现仍然无法解决问题. 仔细一想,发现win10系统的防火墙默默把它 ...

  5. 买or不买?如何测试博彩公司赔率是否合理?

    世界杯期间,烧烤店.酒吧都热闹起来了,柔柔我的朋友圈也热闹起来了,有酱紫的: 还有酱紫的: 然后还有酱紫的: 酱紫的: 当然天台也是一如既然的热闹: 似乎人人都在输钱,那真正的赢家在哪里呢?博彩业的真 ...

  6. 【Tomcat】Tomcat工作原理

    Tomcat 总体结构 Tomcat 的结构很复杂,但是 Tomcat 也非常的模块化,找到了 Tomcat 最核心的模块,您就抓住了 Tomcat 的“七寸”.下面是 Tomcat 的总体结构图: ...

  7. 【Java每日一题】20170223

    20170222问题解析请点击今日问题下方的“[Java每日一题]20170223”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...

  8. jQuery根据元素值或元素下标来删除一个数组元素及数组对象方法列表

    在前提不知道b在这个数组的下标,删除b这个元素  var arrList = ['a','b','c','d'];         arrList.splice(jQuery.inArray('b', ...

  9. awesome python 中文版 相见恨晚!

    awesome python 中文版 相见恨晚!   https://www.zhihu.com/question/24590883 这篇知乎厉害了!一定要学习! 作者:知乎用户链接:https:// ...

  10. js 简单日历

    源地址:https://jingyan.baidu.com/article/546ae185fa4f721149f28cbf.htm 文件:index.htm <!DOCTYPE html> ...