题意:给出n个点和m个边及起始点(相当于此问题中的出口点),求从起始点可到达的点的个数

思路:因为出口点为从其它点通过一些边后到达的终点,因此在添加边关系时,起点转为终点,终点转为起点。

代码如下:

#include<iostream>
#include <vector>
#include <queue>
#include <set>
#include <fstream>
#include <cstring>using namespace std;const int N = 110;
const int INF = 0x3fffffff;class Solution
{
public:void init(int n, int e, int t){this->n = n;this->e = e;this->t = t;for (int i = 0; i < n; i++) adjList[i].clear();edges.clear();}void addEdge(int u, int v, int t){Edge edge = {v, u, t};edges.push_back(edge);int size = edges.size();adjList[v - 1].push_back(size - 1);}int dijkstra(int s){for (int i = 0; i < n; i++){d[i] = INF;}d[s - 1] = 0;priority_queue<HeapNode> pq;pq.push({0, s - 1});set<int> done;while (!pq.empty()){HeapNode curNode = pq.top(); pq.pop();if (done.count(curNode.u)) continue;done.insert(curNode.u);int u = curNode.u;for (size_t i = 0; i < adjList[u].size(); i++){Edge& e = edges[adjList[u][i]];if (d[e.from - 1] + e.cost < d[e.to - 1] && d[e.from - 1] + e.cost <= t){d[e.to - 1] = d[e.from - 1] + e.cost;pq.push({d[e.to - 1], e.to - 1});}}}int cnt = 0;for (int i = 0; i < n; i++){if (d[i] != INF) cnt++;}return cnt;}private:class Edge{public:int from, to, cost;};class HeapNode{public:int d, u;bool operator < (const HeapNode& other) const{return d > other.d;}};private:int n, e, t;vector<Edge> edges;vector<int> adjList[N];int d[N];
};int main()
{Solution solver;int cases;#ifndef ONLINE_JUDGEifstream fin("f:\\OJ\\uva_in.txt");streambuf* old = cin.rdbuf(fin.rdbuf());
#endifcin >> cases;while (cases--){int n, e, t, m;cin >> n >> e >> t;solver.init(n, e, t);cin >> m;while (m--){int u, v, t;cin >> u >> v >> t;solver.addEdge(u, v, t);}int cnt = solver.dijkstra(e);cout << cnt << endl;if (cases) cout << endl;}
#ifndef ONLINE_JUDGEcin.rdbuf(old);
#endifreturn 0;
}

floyd_warshall算法实现如下:

#include <cstdio>
#include <fstream>
#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 110;
const int INF = 0x3fffffff;class Solution
{
public:void init(int n, int e, int t){this->n = n;this->e = e;this->t = t;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (i != j)f[i][j] = INF;elsef[i][j] = 0;}}}void addEdge(int u, int v, int t){f[u - 1][v - 1] = t;}int floyd_warshall(){memcpy(dp, f, sizeof(f));for (int k = 0; k < n; k++){for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (dp[i][k] != INF && dp[k][j] != INF){dp[i][j] = min(dp[i][k] + dp[k][j], dp[i][j]);}}}}int ans = 0;for (int i = 0; i < n; i++){if (dp[i][e - 1] <= t){ans++;}}return ans;}private:int n, e, t;int f[N][N];int dp[N][N];};int main()
{
#ifndef ONLINE_JUDGEifstream fin("D:\\program\\clion\\spoj.txt");streambuf* old = cin.rdbuf(fin.rdbuf());
#endifSolution solver;int cases;cin >> cases;while (cases--){int n, e, t, m;cin >> n  >> e >> t;solver.init(n, e, t);cin >> m;while (m--){int u, v, t;cin >> u >> v >> t;solver.addEdge(u, v, t);}int ans = solver.floyd_warshall();cout << ans << endl;if (cases) cout << endl;}#ifndef ONLINE_JUDGEcin.rdbuf(old);
#endifreturn 0;
}

UVa1112 - Mice and Maze(Dijkstra和Floyd_warshall)相关推荐

  1. yp北京理工 拓扑排序+最短路(更新中re)

    contents: 写在前面 拓扑排序 代码 对应题目 AC代码 最短/长路 无权最短路 无权最长路 带权最短路 带权最长路 spfa 优先对列优化dijkstra BELLMAN_FORD FLOY ...

  2. spring中controller

    提示:原网站已由百度转码,以便在移动设备上查看. 第七城市 (Portal 开发读书笔记)Spring Portlet MVC 测试Controller 2012-04-28 16:32:44 - - ...

  3. Dijkstra 最短路径算法详解 无向图

    对于最短路径问题,这里介绍一种O(N^2)的求解方法.    对于求最短路径的问题一般都会给出一幅图,或者边与边的关系.如上图. 假设我们起点是A,我们要求到F的最短距离,我们会怎么做?  首先,因为 ...

  4. 【CodeForces - 545 ABCDE套题训练题解】贪心, 构造,模拟,dp,最短路树(Dijkstra+变形)

    A: 题干: Input The first line contains integer n (1 ≤ n ≤ 100) - the number of cars. Each of the next  ...

  5. 【qduoj - 1121】小明的贪心题(Dijkstra最短路 + 最短路条数)

    题干: 小明的贪心题 描述 小明来到青岛上学已经一年了,他给青岛这座城市画了一张地图.在这个地图上有n个点,小明的起始点为1号点,终点为n号点,并且地图上的所有边都是单向的.小明知道从i号点到j号点的 ...

  6. 【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

    题干: 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:& ...

  7. 【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

    题干: Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese charac ...

  8. 【POJ - 3037】Skiing (Dijkstra算法)

    题干: Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Be ...

  9. 【HDU - 2066】:一个人的旅行(Dijkstra算法)

    题干: 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景 ...

最新文章

  1. Node.js process 模块常用属性和方法
  2. Android Activity初探
  3. DipperRiver.Net通信协议设计
  4. 使用scatter()绘制散点图
  5. Transformer可以不需要Softmax?Kakao提出了UFO-ViT,性能高,计算量还小
  6. 如果软件也玩凡尔赛文学,将如何对话?
  7. c语言工程作业,西工大c语言程序作业
  8. 我为中国火星第一图做鱼眼矫正
  9. java毕业设计户籍管理系统mybatis+源码+调试部署+系统+数据库+lw
  10. CAD如何绘制带有弧形的箭头
  11. 飞机有“站票”?英乘客机票座位不存在 无奈坐地板
  12. 一款自制calendar插件
  13. 智能驾驶汽车之自动泊车发展阶段
  14. 为什么mysql默认事务隔离级别为RR
  15. Assimp库调用mtl加载obj模型
  16. python列表同时添加多个元素_python怎么向列表中添加多个元素
  17. MySQL常用类型转换函数总结
  18. Qt如何设置控件字体有下划线
  19. 巩膜分割论文:ScleraSegNet: an Improved U-Net Model with Attention for Accurate Sclera Segmentation
  20. Android第三方集成之一键分享ShareSDK的使用eclipse篇 #CSDN博文精选# #IT# #第三方集成# #安卓#

热门文章

  1. Homebrew--MacOSX下的套件管理器
  2. ios开发-UI基础-应用管理(单纯界面)
  3. 排错-Loadrunner录制打不开浏览器解决方法
  4. Linux oracle数据库自动备份自动压缩脚本代码
  5. HashMap数据类型使用注意-不能使用基本数据类型
  6. Chapter10:观察者模式
  7. python基础教程第二版和第三版哪个好-python基础教程 2版和3版哪个适合新手?!...
  8. python爬虫实例-10个python爬虫入门实例
  9. python常用函数-python常用函数精讲
  10. python爬虫框架排行榜-常用python爬虫框架整理