1003 Emergency Dijkstra
这题做的心很累,我用的还是 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,
#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的更多相关文章
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 Emergency[图论]
1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...
- 1003 Emergency (25 分)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- 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 ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
- PAT 甲练习 1003 Emergency
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- 1003 Emergency (25分) 求最短路径的数量
1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of ...
- PAT Advanced 1003 Emergency 详解
题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...
随机推荐
- 如何在ubuntu中安装php
如何在ubuntu中安装php 情衅 | 浏览 692 次 发布于2016-05-07 12:36 最佳答案 关于Ubuntu下的LAMP配置步骤: 首先要安装LAMP 就是Apache,PH ...
- python入门(十三):面向对象(继承、重写、公有、私有)
1. 三种类定义的写法 class P1:#定义类 加不加()都可以 pass class P2(): #带(),且括号中为空,类定义 pass ...
- 工程启动没有报错,但是dubbo后台显示没有提供者,工程没有提供服务
先说一下我遇到的问题:服务工程启动没有异常,消费者工程启动会出现很多nested(嵌套的)错误,但其根本错误是No provider available(缺少服务提供者).可是服务工程起来的时候明明没 ...
- React Router的Route的使用
Route 是 React Router中用于配置路由信息的组件,每当有一个组件需要根据 URL 决定是否渲染时,就需要创建一个 Route. 1) path 每个 Route 都需要定义一个 pat ...
- DNS 原理
一.DNS 是什么? DNS (Domain Name System 的缩写)的作用非常简单,就是根据域名查出IP地址.你可以把它想象成一本巨大的电话本. 举例来说,如果你要访问域名math.stac ...
- linux上安装MongoDB副本集(带keyfile安全认证以及用户权限)
搭建前准备 MongoDB版本:4.0 主要参考搭建MongoDB副本集网站:https://www.jianshu.com/p/f021f1f3c60b 安装之前最好先确定一下几点: 防火墙关闭 M ...
- iserver频繁崩溃、内存溢出事故解决小记
一.事故分析 在生产项目中,频繁遇到iserver隔一段时间就会出现崩溃的情况. 将iserver错误日志发给技术客服后,说是内存溢出的问题. 查看服务器的配置是32g内存,按理说不该出现此类问题. ...
- python_appium_模拟器启动app进行登录
#coding=utf-8from appium import webdriverimport timedesired_caps = {} #列表desired_caps['platformName' ...
- fmt.printf输出的格式
动 词 功 能 %v 按值的本来值输出 %+v 在 %v 基础上,对结构体字段名和值进行展开 %#v 输出 Go 语言语法格式的值 %T 输出 Go 语言语法格式的类型和值 %% 输出 % 本体 %b ...
- linux学习第十五天 (Linux就该这么学) 找到一本不错的Linux电子书,附《Linux就该这么学》章节目录
今天收尾DNS内容复习了,还有分享解析配置,都没有记,主要访问同一个域名,就近访问,