这题做的心很累,我用的还是 1018的思路做的,但是 使用dfs 求最大人数对于某些有问题(现在也不知道错哪了),

看了别人的代码后才发现,其实完全不用这么麻烦,只需设置一个点的权重,一遍DJ(自创简称)下来,直接出答案

收获:

1.DJ不需要存储具体路径也能知道最短路径有几条:

在DJ外设置一个road【n】数组,初始为0;road【源】=1;//road表示,从源出发,到n的最短路径条数

在更新环节

for()

{

  if当dis[u] + e[u][v] < dis[v]

    road【v】=road【u】;

  else if当dis[u] + e[u][v] == dis[v]

    road【v】=road【v】+road【u】;    //通过U到V的路,加上不经过U到V的条数

}//每个road【i】就是从源到i的最短路径条数了

2.使用点权解决不需要输出路径的问题

我在某大神的DJ算法中更新dis的部分看到了这样一句话

for(int v = 0; v < n; v++) {//已经找到一个集合外的最小点 u,

            if(visit[v] == false && e[u][v] != inf) {   《-------这一句判断,不加这一句就是正常的思路,//对于既不是集合内的点,从U出发又不可达的点就跳过,感觉没有必要
                if(dis[u] + e[u][v] < dis[v]) {      //因为这句判断本身已经要对所有输入都判断一次了
                    dis[v] = dis[u] + e[u][v];
                }
            }
 
 
我的部分正确代码:
 #include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int inf=;
int e[][];
int man[]={};
int N,M,C1=,C2;
int a,b,c;
int num=,most=;//最短路径数,最大人数
vector<int> pre[];
vector<int> path,tmppath;
void dfs(int x)//由于存的是到达x的最短路径的前一个点,所以从后往前走C2起步
{
tmppath.push_back(x);
if(x==C1)
{
num++;
a=;
for(int i=tmppath.size()-;i>=;i--)//i初始化为路径上C1的下一个点
{
a+=man[i];
}
if(a>most)
{
path=tmppath;
most=a;
}
}
else
{
for(int i=;i<pre[x].size();i++)
{
dfs(pre[x][i]);
}
}
tmppath.pop_back();
} int main()
{
scanf("%d%d%d%d",&N,&M,&C1,&C2);
int dis[N];//DJ 距离
int mark[N];//DJ 标记
for(int i=;i<N;i++)
{
for(int j=;j<N;j++)
e[i][j]=inf;
dis[i]=inf;
scanf("%d",&man[i]);
mark[i]=; }
for(int i=;i<M;i++)
{
scanf("%d%d%d",&a,&b,&c);
e[a][b]=e[b][a]=c; }
dis[C1]=;//DJ初始化
int now=C1,fast;
for(int i=;i<N;i++)//每次加入一个点
{
fast=inf;
for(int j=;j<N;j++)
{
if(dis[j]<fast&&mark[j]==)//dis 小于当前发现的最小,并且没有被加入
{
now=j;
fast=dis[j];
}
}
if(fast==inf)break;//所有可达点都加入了
mark[now]=;//加入该点
for(int j=;j<N;j++)//用当前该点更新dis,并且更新pre
{
if(dis[now]+e[j][now]<dis[j])
{
dis[j]=dis[now]+e[j][now];
pre[j].clear();
pre[j].push_back(now);
}
else if(dis[now]+e[j][now]==dis[j])
{
pre[j].push_back(now);
}
}
}//DJ 结束,dis[C2],为最短距离
dfs(C2);//使用深搜 求出最短路径的条数 和 最大人数
printf("%d %d",num,most); return ;
}

mycode

正确代码:

#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 ;
}

right code

 
 
 
 
 
 
 
 

1003 Emergency Dijkstra的更多相关文章

  1. PAT 解题报告 1003. Emergency (25)

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

  2. PAT 1003. Emergency (25)

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

  3. PAT 1003 Emergency[图论]

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

  4. 1003 Emergency (25 分)

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

  5. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

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

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

  7. PAT 甲练习 1003 Emergency

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

  8. 1003 Emergency (25分) 求最短路径的数量

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

  9. PAT Advanced 1003 Emergency 详解

    题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...

随机推荐

  1. Multiple dex files define Lcom/google/gson/internal/Streams$AppendableWriter$CurrentWrite;

    开发中引入第三方 aar 时编译同过,运行时出现问题: Multiple dex files define Lcom/google/gson/internal/Streams$AppendableWr ...

  2. 【第二组】Hunter-alpha版本发布报告

    Alpha版本测试报告 一  BUG汇总 1.暂时无法进行注册.(打算修复) 2.用户发布任务界面图标按钮存在显示bug.(打算修复) 3.主界面下拉菜单暂无内容,无法弹出.(打算修复) 二  场景测 ...

  3. TCP/IP的4层模型

    1.网络接入层:将需要相互连接的节点接入网络中,从而为数据传输提供条件: 2.网际互联层:找到要传输数据的目标节点: 3.传输层:实际传输数据: 4.应用层:使用接收到的数据: 形象一点的介绍:整个分 ...

  4. angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?

    1,错误如图所示 简单说下错误原因是:没有js没有注册进去. 解决方法: 1.看下index.html有没有引入你的js文件. 2.看下app.js有没有注册js,比如我这次就是这步没做好,合并代码时 ...

  5. Set集合特点

    1,无序(存储和读取的顺序可能不一样) 2,不允许重复(要求元素唯一) 3,无索引

  6. React-Native android 开发者记录

    1.安装 安装步骤不多废话,按照官网步骤执行即可 安装完之后,react-native run-android发现报错,页面出不来 Error: Unable to resolve module `. ...

  7. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  8. python基础 ---- 安装

    ------  安装两个软件就行了 1.Anaconda   地址:  作用: 管理不同版本的python 的第三方包 下载第三方依赖包和构造版本开发环境 2.python常用的IDE环境 2.1 P ...

  9. Left Join B表,只取B表一条记录

    --用OUTER APPLY select b.* FROM a表 a OUTER APPLY () * from b表 WHERE [Name] = a.[AName] ORDER BY BNo d ...

  10. https与http的区别

    HTTPS协议 由于http协议中,服务器和客户端之间传输的报文是明文,很容易被攻击截取,为了保证一些敏感信息的安全,https协议在http的基础上加了一层SSL协议,依靠证书来保证服务器和客户端之 ...