poj3662 最短路+二分
//Accepted 508 KB 79 ms
//spfa+二分
//二分需要的花费cost,把图中大于cost的边设为1,小于cost的边设为0,然后spfa求
//最短路,如果小于K则可行,继续二分
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
/**
* This is a documentation comment block
* 如果有一天你坚持不下去了,就想想你为什么走到这儿!
* @authr songt
*/
;
;
;
struct node
{
int u,v,c;
node()
{
}
node(int u,int v,int c):u(u),v(v),c(c)
{
}
}p[imax_e];
int e;
bool vis[imax_n];
int head[imax_n];
int next[imax_e];
int dis[imax_n];
int n,m,K;
void init()
{
memset(head,-,sizeof(head));
memset(next,-,sizeof(next));
e=;
}
void addEdge(int u,int v,int c)
{
p[e]=node(u,v,c);
next[e]=head[u];
head[u]=e++;
}
bool relax(int u,int v,int c)
{
if (dis[v]>dis[u]+c)
{
dis[v]=dis[u]+c;
return true;
}
return false;
}
queue<int > q;
bool spfa(int src,int len)
{
while (!q.empty()) q.pop();
memset(vis,false,sizeof(vis));
;i<=n;i++)
{
dis[i]=inf;
}
dis[src]=;
q.push(src);
vis[src]=true;
while (!q.empty())
{
int pre=q.front();
q.pop();
vis[pre]=false;
;i=next[i])
{
:;
if (relax(pre,p[i].v,c) && !vis[p[i].v])
{
vis[p[i].v]=true;
q.push(p[i].v);
}
}
}
if (dis[n]<=K) return true;
else return false;
}
int main()
{
while (scanf("%d%d%d",&n,&m,&K)!=EOF)
{
init();
int u,v,c;
,right=,mid;
;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
right=right>c?right:c;
addEdge(u,v,c);
addEdge(v,u,c);
}
,right)==)
{
printf("-1\n");
continue ;
}
while (left<=right)
{
mid=(left+right)/;
//printf("left=%d right=%d mid=%d\n",left,right,mid);
,mid))
{
right=mid-;
}
else
{
left=mid+;
}
}
printf();
}
}
poj3662 最短路+二分的更多相关文章
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- Luogu P1462 通往奥格瑞玛的道路(最短路+二分)
P1462 通往奥格瑞玛的道路 题面 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己 ...
- poj-3662 Telephone Lines 二分答案+最短路
链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...
- hdu2962 Trucking (最短路+二分查找)
Problem Description A certain local trucking company would like to transport some goods on a cargo t ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)
Occupy Cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- CSU 1307 最短路+二分
题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或 ...
- 洛谷1462 通往奥格瑞玛的道路 最短路&&二分
SPFA和二分的使用 跑一下最短路看看能不能回到奥格瑞玛,二分收费最多的点 #include<iostream> #include<cstdio> #include<cs ...
随机推荐
- htmlunit官网简易教程(翻译)
1 环境搭建: 1)下载 从链接:http://sourceforge.net/projects/htmlunit/files/htmlunit/ 下载最新的bin文件 2)关于bin文件 里面主要包 ...
- SQL SERVER 2005 DBCC IND命令说明
每天笑一笑,烦恼少一倍 轻松一笑!狗狗被调戏:http://947kan.com/video/player-52952-0-0.html ------------------------------- ...
- iOS 架构模式--解密 MVC,MVP,MVVM以及VIPER架构
本文由CocoaChina译者lynulzy(社区ID)翻译 作者:Bohdan Orlov 原文:iOS Architecture Patterns 在 iOS 中使用 MVC 架构感觉很奇怪? 迁 ...
- SharePoint 根据时间筛选
最近在整SP列表 老大要求用列表规范周报格式. 提出要在一个视图内查看上周一至周日的内容 翻了下资料想到了以下几种方法 1.在视图页面添加时间筛选器webpart,用参数传入列表筛选 2.在列表添加按 ...
- css中的一些概念
1.伪类与伪元素 1.单冒号(:)用于 CSS3 伪类,双冒号(::)用于 CSS3 伪元素. 2.对于 CSS2 中已经有的伪元素,例如 :before,单冒号和双冒号的写法 ::before 作用 ...
- ElasticSearch部署安装
测试版本:elasticsearch-5.1.1 1.Windows环境下安装(win10系统) 1)解压elasticsearch-5.1.1.zip. 2)执行elasticsearch.bat启 ...
- tcpdump 获取http请求url
There are tcpdump filters for HTTP GET & HTTP POST (or for both plus message body): Run man tcpd ...
- 【小月博客】 Html5 上传图片 移动端、PC端通用
在博客园注册账号有些天了,感觉有些许欣慰,自己写的东西有人在看,有人在评论很是开心.(ps: 满足一下虚荣心吧!) 废话不多说了,说一下今天给大家分享的是 html5上传图片.我们是在移动端使用的,但 ...
- Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 总结4点对学习Linux有帮助的建议(纯干货)
学习需要足够的毅力和耐心 有些人把Linux运维看作一项冗长而枯燥的工作:有些人把linux运维看作一项得力的工具.如果是前者建议还是改变一下认识,不然不建议入门这行.毕竟linux运维工作是对人的毅 ...