1003. Emergency (25)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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

题意:n个城市,m条边,每条边有边权,每个城市有点权,最短路可能有多条,要求出路径最短的情况下的点权最大的路径,并输出,还要输出最短路径的条数。

思路:可以用迪杰斯特拉也可以用贝尔曼算法解决。还可以用深搜。

迪杰斯特拉:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3fffffff
const int N=505;
int m,n,c1,c2,e[N][N],dis[N],num[N],pit[N],sum[N];
bool vis[N];
void dijkstra()
{fill(dis,dis+N,inf);memset(vis,false,sizeof(vis));memset(num,0,sizeof(num));//最短路径数量memset(sum,0,sizeof(sum));//点权dis[c1]=0;num[c1]=1;sum[c1]=pit[c1];for(int i=0;i<n;i++){int tmp=inf,u=-1;for(int j=0;j<n;j++){if(!vis[j]&&dis[j]<tmp){tmp=dis[j];u=j;}}if(u==-1) return ;vis[u]=true;for(int j=0;j<n;j++){if(!vis[j]&&e[u][j]!=inf){if(dis[u]+e[u][j]<dis[j]){dis[j]=dis[u]+e[u][j];num[j]=num[u];sum[j]=sum[u]+pit[j];}else if(dis[u]+e[u][j]==dis[j]){num[j]+=num[u];if(sum[u]+pit[j]>sum[j])sum[j]=sum[u]+pit[j];}}}}
}
int main()
{while(~scanf("%d%d%d%d",&n,&m,&c1,&c2)){fill(e[0],e[0]+N*N,inf);int a,b,c;for(int i=0;i<n;i++)scanf("%d",&pit[i]);for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);e[a][b]=e[b][a]=c;}dijkstra();printf("%d %d\n",num[c2],sum[c2]);}
}

贝尔曼

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3fffffff
const int N=510;
struct node
{int v,dis;node(int _v,int _dis) : v(_v),dis(_dis) {}//构造函数
};
vector<node> adj[N];//邻接表存图
int dis[N],num[N],sum[N],pit[N],m,n,c1,c2;
set<int> pre[N];
void bell()
{fill(dis,dis+N,inf);memset(num,0,sizeof(num));;memset(sum,0,sizeof(sum));dis[c1]=0;num[c1]=1;sum[c1]=pit[c1];for(int i=0;i<n-1;i++){for(int u=0;u<n;u++)//转折点{for(int j=0;j<adj[u].size();j++){int v=adj[u][j].v;//终点int diss=adj[u][j].dis;if(dis[u]+diss<dis[v]){dis[v]=dis[u]+diss;num[v]=num[u];sum[v]=sum[u]+pit[v];
//                    sum[v]+=pit[u];//这样写也对pre[v].clear();pre[v].insert(u);}else if(dis[u]+diss==dis[v]){if(sum[u]+pit[v]>sum[v])sum[v]=sum[u]+pit[v];pre[v].insert(u);num[v]=0;//遇到相同的最短路,将路径数清零set<int>::iterator it;for(it=pre[v].begin();it!=pre[v].end();it++)num[v]+=num[*it];//遍历前驱结点,将所有前驱结点的路径数量加起来}}}}
}
int main()
{while(~scanf("%d%d%d%d",&n,&m,&c1,&c2)){int u,v,wt;for(int i=0;i<n;i++)scanf("%d",&pit[i]);for(int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&wt);adj[u].push_back(node(v,wt));adj[v].push_back(node(u,wt));}bell();printf("%d %d\n",num[c2],sum[c2]);}
}

PAT 1003 Emergency(最短路(迪杰斯特拉||贝尔曼)最小边权下的最大点权)相关推荐

  1. 7-38 社交网络图中结点的“重要性”计算 (30分) 最短路 迪杰斯特拉堆优化

    在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互作用,可以增强也可以减弱.而结点根据其所处的位置不同,其 ...

  2. HUD 2544 最短路 迪杰斯特拉算法

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. 迪杰斯特拉算法及变式(最短距离,打印路径,最短经过节点数)

    问题描述 给定一个图,图的节点名称用(000 ~ N−1N - 1N−1)表示.NNN为图的节点个数,MMM为边的个数,SSS为起始点. 输入条件: 第一行输入 NMSN M SNMS. 其后MMM行 ...

  4. [Unity]寻路算法:广度优先、迪杰斯特拉、启发式、A星

    以下是通过阅读这个链接(写的真挺不错,而且作者还提供了一些可视化的操作,方便理解各个算法的异同,强烈推荐),自己收获的一些浅俗的理解和总结.如有偏差和错误还望评论区指正. 1.Breadth Firs ...

  5. 算法学习--迪杰斯特拉和弗洛伊德

    一.迪杰斯特拉 求解图中由起点开始到其他节点的最短路径问题(权值不能为负). 主要思想:在有权值的图中,直接和起点相连的节点他们的最短路径为和起点相连的边的权值,和起点没有直接相连的节点默认为该点和起 ...

  6. 最短路径算法 迪杰斯特拉、佛洛依德和贝尔曼

    最短路径算法 迪杰斯特拉算法 佛洛依德算法 迪杰斯特拉算法 迪杰斯特拉算法用来解决在有向有权图中某一个点到任意一点的最短路径问题. 注意:只能用来解决权为非零的情况,不能够解决权为负数的情况 思想:我 ...

  7. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

  8. (迪杰斯特拉)Dijkstra算法详解 PAT甲级 1003

    1.迪杰斯特拉(Dijkstra)算法介绍 迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最 短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想) ...

  9. pat1003 迪杰斯特拉法和dfs求最短路

    本题的背景是求定点和定点之间的最短路问题(所有的最短路 不是一个解  是全部解,方法手段来自数据结构课程中的迪杰斯特拉算法和dfs(深度优先遍历). 分别用两种方法编程如下代码 dfs #includ ...

最新文章

  1. 回《笔试常见的“阶乘”编程题,你写对了么?》
  2. 基于小波变换的图像压缩解压缩仿真
  3. html ppt文件在线播放,[2018年最新整理]如何在PPT中插入html网页.ppt
  4. 如何在servlet刚启动时候获取服务器根目录?
  5. P3306-[SDOI2013]随机数生成器【BSGS】
  6. 前端学习(1670):前端系列实战课程之核心运动原理
  7. arduino 光控灯_Arduino光控开关
  8. 期刊缩写查询_干活分享——SCI期刊名英文缩写查询
  9. 5年iPhone用户换小米11 Ultra:惊叹小米变化大
  10. Oracle基础篇--01数据库控制语言DCL
  11. CNN for Sentence Classification-textcnn阅读笔记
  12. 解决屏蔽JS代码报错的问题
  13. HTML超链接使用代码
  14. echarts2获取series下的data数值
  15. 《Adobe Photoshop CS5中文版经典教程(全彩版)》—第2课2.3节概述
  16. .Net -- EF Core详解
  17. 扫描枪识别条码为乱码
  18. 思维模型 5W2H分析法
  19. 一个故事,讲懂什么是区块链
  20. 为什么现在用的otm8018b型LCD屏的ID不能被读取?

热门文章

  1. Jdbc小项目:员工工管理系统
  2. 网络授时 linux,关于“网络授时域名”全面试运行测试的公告
  3. RTS相机+边框限制
  4. 例说linux内核与应用数据通信(三):读写内核设备驱动文件
  5. 「多校联考」第三周二场
  6. vue-weex 仿穷游APP
  7. 量子计算机和超导,量子计算的未来在哪里——超导电路与光子学
  8. 生命有一种负重叫时间
  9. 由微信2019公开课学到的
  10. macos iTerm2 优化