题目链接:Sightseeing

题意:求最短路和比最短路长度+1的所有路径条数。

附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
#define maxn 1010 struct Node {
int to, val;
Node(int to, int val) {
this->to = to;
this->val = val;
}
};// 保存每个点的所有边及其权值 vector<Node> edge[maxn]; int step[maxn][2]; //bushu[i][j] 表示i点第j短路的长度
int cnt[maxn][2]; // cnt[i][j]表示i点第j短路的条数 int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d%d", &n, &m); for (int i=1; i<=n; ++i) {
edge[i].clear();
step[i][0] = maxn*maxn;
step[i][1] = maxn*maxn;
cnt[i][0] = 0;
cnt[i][1] = 0;
} for (int i=0; i<m; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
edge[x].push_back(Node(y, z));
} int st, ed;
scanf("%d%d", &st, &ed); step[st][0] = 0; // 初始化源点
step[st][1] = maxn*maxn;
cnt[st][0] = 1;
cnt[st][1] = 0; bool ok = false; while(1) {
ok = false;
for (int i=1; i<=n; ++i) {
for (int k=0; k<2; ++k) {
if (cnt[i][k] && i != ed) {
for (int j=0; j<edge[i].size(); ++j) {
int to = edge[i][j].to;
int val = edge[i][j].val; if (step[to][0] > step[i][k] + val) { // 更新最短路
step[to][1] = step[to][0];
cnt[to][1] = cnt[to][0];
step[to][0] = step[i][k] + val;
cnt[to][0] = cnt[i][k];
ok = true;
}
else if (step[to][0] == step[i][k] + val) {
cnt[to][0] += cnt[i][k];
ok = true;
}
else if (step[to][1] > step[i][k] + val) { // 更新次短路
step[to][1] = step[i][k] + val;
cnt[to][1] = cnt[i][k];
ok = true;
}
else if (step[to][1] == step[i][k] + val) {
cnt[to][1] += cnt[i][k];
ok = true;
}
}
cnt[i][k] = 0;
}
}
}
if (ok == false) break;
} // for (int i=1; i<=n; ++i) {
// cout << i << "==" << step[i][0] << " " << cnt[i][0] << " " << step[i][1] << " " << cnt[i][1] << endl;
// }
int ans = cnt[ed][0];
if (step[ed][1] == step[ed][0] + 1) {
ans += cnt[ed][1];
}
printf("%d\n", ans);
}
return 0;
}
/*
HDU 1688
*/ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#define maxn 1010
#include <queue>
using namespace std; struct Node {
int v, w;
Node(int v, int w) {
this->v = v;
this->w = w;
}
}; vector <Node> edge[maxn];
int st, ed;
int step[maxn][2];
int cnt[maxn][2];
int mp[maxn][maxn]; void bfs(int st) {
queue<int> que;
que.push(st);
while(!que.empty()) {
int now = que.front();
que.pop();
// cout << now << "-----------------\n";
for (int k=0; k<2; ++k) {
if (cnt[now][k] && now != ed) {
for (int j=0; j<edge[now].size(); ++j) {
int v = edge[now][j].v;
int val = edge[now][j].w;
if (step[v][0] > step[now][k] + val) {
step[v][1] = step[v][0];
cnt[v][1] = cnt[v][0];
step[v][0] = step[now][k] + val;
cnt[v][0] = cnt[now][k];
que.push(v);
// cout << v << "==1==" << step[v][0] << " " << cnt[v][0] << " " << step[v][1] << " " << cnt[v][1] << endl;
}
else if (step[v][0] == step[now][k] + val) {
cnt[v][0] += cnt[now][k];
que.push(v);
// cout << v << "==2==" << step[v][0] << " " << cnt[v][0] << " " << step[v][1] << " " << cnt[v][1] << endl;
}
else if (step[v][1] > step[now][k] + val) {
step[v][1] = step[now][k] + val;
cnt[v][1] = cnt[now][k];
que.push(v);
// cout << v << "==3==" << step[v][0] << " " << cnt[v][0] << " " << step[v][1] << " " << cnt[v][1] << endl;
}
else if (step[v][1] == step[now][k] + val) {
cnt[v][1] += cnt[now][k];
que.push(v);
//cout << v << "==4==" << step[v][0] << " " << cnt[v][0] << " " << step[v][1] << " " << cnt[v][1] << endl;
}
}
cnt[now][k] = 0;
}
}
}
} int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
edge[i].clear();
step[i][0] = maxn*maxn;
step[i][1] = maxn*maxn;
cnt[i][0] = 0;
cnt[i][1] = 0;
} for (int i=0; i<m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
edge[u].push_back(Node(v, w));
} scanf("%d%d", &st, &ed);
step[st][0] = 0;
cnt[st][0] = 1; bfs(st);
//
// for (int i=1; i<=n; ++i) {
// cout << i << "==" << step[i][0] << " " << cnt[i][0] << " " << step[i][1] << " " << cnt[i][1] << endl;
// } int ans = cnt[ed][0];
if (step[ed][1] == step[ed][0] + 1) {
ans += cnt[ed][1];
}
printf("%d\n", ans);
}
return 0;
} /*
99
3 3
1 2 1
2 3 1
1 3 1
1 3 */

  

HDU 1688 Sightseeing的更多相关文章

  1. 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 ...

  2. HDU 1688 Sightseeing 【输出最短路+次短路条数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...

  3. hdu 1688 Sightseeing (最短路径)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 【最短路】HDU 1688 Sightseeing

    题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...

  5. poj 3463/hdu 1688 求次短路和最短路个数

    http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...

  6. 【HDOJ】1688 Sightseeing

    Dijkstra求解次短路径,使用cnt和dis数组记录最小.次小的个数和长度.重写更新操作. /* 1688 */ #include <iostream> #include <st ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

随机推荐

  1. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. iOS - UISlider

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl <NSCoding> @a ...

  3. mysql中替换行首字符

    替换行首字符,而不替换字段中其它地方指定字符. UPDATE table SET open_time = CONCAT('W', open_time) WHERE open_time REGEXP ' ...

  4. 16位的MD5加密和32位MD5加密的区别

    16位的MD5加密和32位MD5加密的区别 MD5加密后所得到的通常是32位的编码,而在不少地方会用到16位的编码它们有什么区别呢?16位加密就是从32位MD5散列中把中间16位提取出来!其实破解16 ...

  5. Git基本交互流程图

  6. 【Todo】秒杀系统材料

    秒杀系统:Link <一个经验证可落地的秒杀系统实践思路> 主要依赖于Redis进行处理. http://geek.csdn.net/news/detail/59847   淘宝大秒系统设 ...

  7. 在beforeAction里redirect无效,Yii2.0.8

    我是在官方GitHub上得到回答,试了一下,确实解决问题了.之前的问题描述: 之前是2.0.3,然后用composer直接升级到2.0.8,就不正常了,以为是我代码的问题,于是再次尝试 用compos ...

  8. JavaMail 发送邮件案例

    #----------------这两个是构建session必须的字段---------- #smtp服务器 mail.smtp.host=smtp.exmail.qq.com #身份验证 mail. ...

  9. spring的初始化bean,销毁bean之前的操作详解

    我所知道的在spring初始化bean,销毁bean之前的操作有三种方式: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二种是 ...

  10. SDL2.0的VS开发环境搭建

    SDL2.0的VS开发环境搭建 [前言] 我是用的是VS2012,VS的版本应该大致一样. [开发环境搭建] >>>SDL2.0开发环境配置:1.从www.libsdl.org 下载 ...