City Tour
Description
Input
Output
对于每组测试数据输出满足Alice要求的从A到B的最短距离。
Sample Input
#include<stdio.h>
#include<string.h>
#define maxn 2020
#define maxm 50500
#define inf 100000000 struct edge
{
int u,v;
int val,next;
} e[*maxm],seq[maxm]; int last[maxn];
int que[maxm],dis[maxn];
bool vis[maxn];
int t,n,m,A,B; void add(int u,int v,int val)//离散化存储
{
e[t].v = v;
e[t].val = val;
e[t].next = last[u];
last[u] = t++;
return ;
}
void build(int val)//建树
{
memset(last,-,sizeof(last));
t = ;
for (int i=; i<=m; i++)
if (seq[i].val <= val)
{
add(seq[i].u,seq[i].v,seq[i].val);
add(seq[i].v,seq[i].u,seq[i].val);
}
return ;
} int spfa()
{//采用负权存储
memset(vis,,sizeof(vis));
for (int i=; i<=n; i++) dis[i] = inf;
int head = , tail = ;
que[++tail] = A;
vis[A] = ;
dis[A] = ;
while (head < tail)
{
int u = que[++head];
for (int j=last[u]; j!=-; j=e[j].next)//遍历与j相连的边
{
int v = e[j].v;
int val = e[j].val;
if (dis[u] + val < dis[v]) //松弛条件判断
{
dis[v] = dis[u] + val;
if (vis[v]==)
{
vis[v] = ;
que[++tail] = v;
}
}
}
vis[u] = ;
}
if (dis[B] < inf) return dis[B];
return -;
}
int solve()
{
int res = -;
int l = , r = ;
while (l <= r)//二分最大边
{
int mid = (l + r)/;
build(mid);
int tmp = spfa();
if (tmp == -) l = mid + ;
else
{//当mid(即每段中的最大值)减少时,res进行更新
res = tmp;
r = mid - ;
}
}
return res;
}
int main()
{
while (scanf("%d%d%d%d",&n,&m,&A,&B)==)
{
for (int i=; i<=m; i++) scanf("%d%d%d",&seq[i].u,&seq[i].v,&seq[i].val);
int ans = solve();
printf("%d\n",ans);
}
return ;
}
图论(二分+SPFA)
Sample Output
City Tour的更多相关文章
- 1307: City Tour
1307: City Tour Time Limit: 1 Sec Memory Limit: 128 MB [Submit][Status][Web Board] Description Alic ...
- HDU 5013 City Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5013 题意: 思路: 这里有错,是Hi(x)=sigama(Hji)(j属于x) const int ...
- 2013 CSU校队选拔赛(1) 部分题解
A: Decimal Time Limit: 1 Sec Memory Limit: 128 MB Submit: 99 Solved: 10 [ Submit][ Status][ Web ...
- 每日英语:How to Save Detroit
Detroit is beautiful-though you probably have to be a child of the industrial Midwest, like me, to s ...
- CSU-1307-二分+dij
1307: City Tour Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 59 ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
随机推荐
- iOS8新特性之基于地理位置的消息通知UILocalNotification
苹果在WWDC2014上正式公布了全新的iOS8操作系统. 界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好. ...
- Spring事务管理使用
发现问题 最近,碰到一个问题,再用spring实现事务管理的时候,发现不起作用,在出异常时,并不会回滚数据库操作. 我想实现的功能如下: @Transactional(isolation=Isolat ...
- 拿到手机ip住址
转载自:http://blog.csdn.net/showhilllee/article/details/8746114 貌似ASI里获取ip地址的链接不能够了.也曾试过whatismyip,在其站点 ...
- C++ inline 函数
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- 0x05: post 守护进程(deamon) json 任务调度
python 签名 post #coding:utf-8 import urllib,urllib2 url='http://wtf.thinkphp.com/index.php?m=&c=t ...
- Java多线程——线程池
系统启动一个新线程的成本是比较高的,因为它涉及到与操作系统的交互.在这种情况下,使用线程池可以很好的提供性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考虑使用线程池. 与数据库连接池类似 ...
- dropdownlist 二级联动
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GradeBind(); } } //绑定 ...
- ManagedPipelineHandler IIS
IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandl ...
- 原生js 学习之array 数组
Array的原生方法: concat(): 连接两个或更多的数组哦 join(): 把数组的所有元素放在一个字符串中 pop():删除并返回数组的最后一个元素 push():向数组的末尾添加一个元素 ...
- 在 ASP.NET 网页中不经过回发而实现客户端回调
一.使用回调函数的好处 在 ASP.NET 网页的默认模型中,用户会与页交互,单击按钮或执行导致回发的一些其他操作.此时将重新创建页及其控件,并在服务器上运行页代码,且新版本的页被呈现到浏览器.但是, ...