Sightseeing,题解
题目:
题意:
找到从s到t与最短路长度相差少于1的路径总数。
分析:
首先,搞明白题意之后,我们来考虑一下怎么处理这个1,怎样找相差为1的路径呢?我们这样想,如果有相差为1的路径,那么它将会是严格的次短路,所以我们可以再跑最短路的时候顺带着跑了次短路(严格的),然后判断一下和最短路是不是相差一然后就计算一下就好了,当然,这个的时间复杂度其实就是常数大了的E*logE的,所以时间复杂度是没问题的,然后就是代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=(1e3)*+;
const int maxm=1e5+;
struct E{
int to;
int next;
int val;
}ed[maxm];
int tot;
int head[maxn];
void J(int a,int b,int c){
tot++;
ed[tot].to=b;
ed[tot].val=c;
ed[tot].next=head[a];
head[a]=tot;
}
struct Node{
int dis;
int x;
int ci;
friend bool operator < (Node a,Node b){
return a.dis>b.dis;
}
Node(int a,int b,int c){
x=a;
dis=b;
ci=c;
}
};
priority_queue<Node> qu;
bool vis[maxn][];
int cot[maxn][];
int dis[maxn][];
int main(){
int t;
scanf("%d",&t);
for(int jsjs=;jsjs<=t;jsjs++){
memset(head,,sizeof(head));
tot=;
memset(vis,,sizeof(vis));
memset(cot,,sizeof(cot));
memset(dis,0x3f,sizeof(dis));
int n,m;
scanf("%d%d",&n,&m);
int js1,js2,js3;
for(int i=;i<=m;i++){
scanf("%d%d%d",&js1,&js2,&js3);
J(js1,js2,js3);
}
int s,q;
scanf("%d%d",&s,&q);
dis[s][]=;
cot[s][]=;
qu.push(Node(s,,));
while(!qu.empty()){
Node js=qu.top();
qu.pop();
if(vis[js.x][js.ci])
continue;
vis[js.x][js.ci]=;
for(int i=head[js.x];i;i=ed[i].next){
int to=ed[i].to;
int di=js.dis+ed[i].val;
if(dis[to][]>di){
dis[to][]=dis[to][];
dis[to][]=di;
cot[to][]=cot[to][];
cot[to][]=cot[js.x][js.ci];
qu.push(Node(to,dis[to][],));
qu.push(Node(to,dis[to][],));
continue;
}
if(dis[to][]==di){
cot[to][]+=cot[js.x][js.ci];
continue;
}
if(dis[to][]>di){
dis[to][]=di;
cot[to][]=cot[js.x][js.ci];
qu.push(Node(to,dis[to][],));
continue;
}
if(dis[to][]==di){
cot[to][]+=cot[js.x][js.ci];
continue;
}
}
}
printf("%d\n",cot[q][]+(dis[q][]==dis[q][]+?cot[q][]:));
}
return ;
}
Sightseeing,题解的更多相关文章
- POJ 3463 Sightseeing 题解
题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...
- POJ3621:Sightseeing Cows——题解
http://poj.org/problem?id=3621 全文翻译参自洛谷:https://www.luogu.org/problemnew/show/P2868 题目大意:一个有向图,每个点都有 ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- Sightseeing Cows(最优比率环)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8915 Accepted: 3000 ...
- TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)
描述 The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that to ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
随机推荐
- (十)DVWA之SQL Injection--测试分析(Impossible)
DVWA之SQL Injection--测试分析(Impossible) 防御级别为Impossible的后端代码:impossible.php <?php if( isset( $_GET[ ...
- Hive的压缩存储和简单优化
一.Hive的压缩和存储 1,MapReduce支持的压缩编码 压缩格式 工具 算法 文件扩展名 是否可切分 对应的编码/解码器 DEFLATE 无 DEFLATE .deflate 否 org.ap ...
- TypeScript使用体会(一)
typescript使用体会 近期接手了一个公司项目是由TS写的,第一次用在这里做一下简单的使用体会 个人觉得TS与JS相差不多,只是多了一些约束(可能自己还没体会到精髓) typescript是Ja ...
- hadoop知识整理(4)之zookeeper
一.介绍 一个分布式协调服务框架: 一个精简的文件系统,每个节点大小最好不大于1MB: 众多hadoop组件依赖于此,比如hdfs,kafka,hbase,storm等: 旨在,分布式应用中,提供一个 ...
- Docker+Selenium+TestNG+Maven+Jenkins环境搭建
一.Selenium环境准备 standalone-chrome Docker容器启动: docker pull selenium/standalone-chrome version: '3' ser ...
- Python函数&异常处理
1. 函数基础 1.1 参数和返回值 1.1.1 参数 位置参数.关键字参数 def my_func1(x, y, z): print(x+y+z, "计算结束") my_func ...
- WINCC 应用与提高(78讲15.98G)视频教程网盘下载
收集与网络,供参考. https://blog.csdn.net/txwtech/article/details/94225748
- Beta冲刺--项目测试
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 Beta冲刺--项目测试 作业正文 如下 其他参考文献 ... Beta冲刺 ...
- SpringBoot 2.0 编程方式配置,不使用默认配置方式
SpringBoot的一般配置是直接使用application.properties或者application.yml,因为SpringBoot会读取.perperties和yml文件来覆盖默认配置: ...
- Python3-shelve模块-持久化字典
Python3中的shelve提供了持久化字典对象 和字典基本一个样,只不过数据保存在了文件中,没什么好说的,直接上代码 注: 1.打开文件后不要忘记关闭文件 2.键只能是字符串,值可以是任何值 3. ...