1003 Emergency (25)(25 分)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.\ All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4
//转自:https://www.cnblogs.com/549294286/p/3571448.html
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; #define INF 0x3f3f3f3f
#define MX 501 int mp[MX][MX];
int v[MX];
int dist[MX];
int amount[MX];
int teams[MX];
int pathcount[MX];
int N,M,start,en; void dijkstra(int s){
amount[s] = teams[s];//s为开始时
dist[s] = ;
pathcount[s] = ;//目前有一条路 while (){
int u, dmin=INF;
for (int i=; i<N; i++){
if (v[i]== && dist[i]<dmin){//v一开始全为0
dmin = dist[i];//第一次循环,选出来的点是s
u = i;//
}
}
if (dmin==INF || u==en) break;
v[u] = ;//设置为被访问过,每次都选最小的那个点出来。
printf("\n");
for (int i=; i<N; i++){
if(v[i]==){
if (dist[i] > dist[u] + mp[u][i]){
dist[i] = dist[u] + mp[u][i];
amount[i] = amount[u] + teams[i];
pathcount[i] = pathcount[u];
}else if (mp[u][i]!=INF&&dist[i] == dist[u] + mp[u][i]){
pathcount[i] += pathcount[u];
if (amount[i] < amount[u] + teams[i])
amount[i] = amount[u] + teams[i];
}
printf("%d %d %d %d %d\n",u,i,dist[i],pathcount[i],amount[i]);
} }
}
} int main()
{
scanf("%d%d%d%d", &N,&M,&start,&en);
for (int i=; i<N; i++)
{
scanf("%d", &teams[i]);//每个点所有的救援队数
}
for (int i=; i<N; i++)
{
dist[i] = INF;//每一个距离初始化为正无穷。
for (int j=; j<N; j++)
mp[i][j] = INF;
}
for (int i=; i<M; i++)
{
int c1, c2, L;
scanf("%d%d%d", &c1,&c2,&L);
mp[c1][c2] = mp[c2][c1] = L;//将没有边的设置为正无穷。
} dijkstra(start);
printf("%d %d", pathcount[en], amount[en]); return ;
}

//意思就是给出一个图,每个点有权值,边有权重,找出最短的权重的边,并且记录点的最大的权值和。就是要找出所有的最短边,并且进行比较。

//这里是给出了一个循环,每次找出最短的,并且遍历所有的点。看是否可以更新距离。如果到当前点的距离相等的话,那么就路径数相加,并且amount[i]取两者较大值。这是一种题型。应该会的。

代码来自:https://www.liuchuo.net/archives/2359

#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int e[][], weight[], dis[], num[], w[];
bool visit[];
const int inf = ;
int main() {
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for(int i = ; i < n; i++)
scanf("%d", &weight[i]);
fill(e[], e[] + * , inf);//原来二维数组是这样初始化的。
fill(dis, dis + , inf);
int a, b, c;
for(int i = ; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[a][b] = e[b][a] = c;
}
dis[c1] = ;
w[c1] = weight[c1];
num[c1] = ;//到当前点的共计路径条数。
for(int i = ; i < n; i++) {
int u = -, minn = inf;
for(int j = ; j < n; j++) {
if(visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
}
if(u == -) break;//迪杰斯特拉都有这么一个判断,就是找不到其他的可以访问的点了。
visit[u] = true;
for(int v = ; v < n; v++) {
if(visit[v] == false && e[u][v] != inf) {
if(dis[u] + e[u][v] < dis[v]) {
dis[v] = dis[u] + e[u][v];
num[v] = num[u];
w[v] = w[u] + weight[v];//点权相加
} else if(dis[u] + e[u][v] == dis[v]) {
num[v] = num[v] + num[u];//如果相等,就把两者相加。
if(w[u] + weight[v] > w[v])
w[v] = w[u] + weight[v];
}
}
}
}
printf("%d %d", num[c2], w[c2]);
return ;
}

//感觉这个代码更好理解一点,就是使用迪杰斯特拉的变形,也就是在更新边的同时加了几个判断条件,通常来说迪杰斯特拉是遍历一遍就会结束了,但是这个加了一个for循环,那么就每次都会选出一个最短的边(到c1,即原点),选出后才会被标记为已经访问过。

PAT 1003 Emergency[图论]的更多相关文章

  1. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  2. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  3. PAT 1003 Emergency

    1003 Emergency (25 分)   As an emergency rescue team leader of a city, you are given a special map of ...

  4. PAT 1003 Emergency 最短路

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  5. PAT 1003 Emergency (25分)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  6. PAT 1003. Emergency 单源最短路

    思路:定义表示到达i的最短路径数量,表示到达i的最短径,表示最短路径到达i的最多人数,表示从i到j的距离, 表示i点的人数.每次从u去更新某个节点v的时候,考虑两种情况: 1.,说明到达v新的最短路径 ...

  7. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  8. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  9. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

随机推荐

  1. JS基础---->javascript的基础(一)

    记录一些javascript的基础知识.只是一起走过一段路而已,何必把怀念弄的比经过还长. javascript的基础 一.在检测一个引用类型值和 Object 构造函数时, instanceof 操 ...

  2. Python Tkinter 学习成果:点歌软件music

    笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV. 硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频.因此点 ...

  3. 动力学仿真引擎ODE的学习笔记,C#演示(一)

    ®版权声明:本文为博主原创文章,未经博主允许不得转载. 一.ODE介绍与平台搭建. 接触到动力学仿真引擎, 是因为笔者的一款PLC仿真软件需要3D仿真.我需要达到的效果是,以3D方式构建出工控行业中常 ...

  4. eclipse中改变默认的workspace的方法

    1.File-->Switch Workspace-->Other 2.Window-->Preferences-->General-->Startup and Shui ...

  5. java(6) ArrayList源码

    系统环境: JDK 1.7 成员变量 //默认的初始化数组大小 private static final int DEFAULT_CAPACITY = 10; //空的对象数组 private sta ...

  6. 题目1208:10进制 VS 2进制(进制转换以及大数保存问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1208 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. sencha touch Demo(示例)(2014-6-25)

    这是一个开源示例,是我对sencha touch的深层应用.已停止更新 sencha touch版本:2.2.1/2.3.1 源码地址: https://bitbucket.org/moLangZai ...

  8. 学习项目部署Django+uwsgi+Nginx生产环境部署

    绪论 项目生产环境部署,是很重的一个知识点.第一,Django自带的服务器很简陋,由于测试和开发环境尚可,无法用于生产环境,保障安全性和可靠性.以及性能.此外,学习部署方式,还有利于了解生产部署后的项 ...

  9. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十九:SDRAM模块② — 多字读写

    实验十九:SDRAM模块② — 多字读写 表示19.1 Mode Register的内容. Mode Register A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A ...

  10. Centos 7.x临时的网络与路由配置

    今天在虚拟机上安装了Centos 7.1操作系统,使用的最小化安装,安装完成后准备使用ifconfig命令时,发现命令不存在,如下: 心想肯定是新版的Centos 系统默认情况下没有使用ifconfi ...