#include <stdio.h>  #define N 501int rescue[N] = {1,2,1,5,3};
int startP,endP;
int res[N] = {0};
int dist[N];//距离值
int path[N]={0};
int size=0;
int vex[N][N]={0};
int visit[N]={0};
int max[N]={0};  void DFS(int start, int end){   int j,i = start;if(start == end){//找到或没找到return;} else {for(j=0;j<size;j++){if(vex[i][j] != 0 && visit[j] == 0){visit[j] = 1;
//              path[j] = path[i];if(dist[j] == dist[i] + vex[i][j]){path[j]++;res[j] = res[i] + rescue[j];if(res[j] > max[j]) {max[j] = res[j];} else{res[j] = max[j];}}if(dist[j] > dist[i] + vex[i][j]){dist[j] = dist[i] + vex[i][j];path[j] = 1;res[j] = res[i] + rescue[j];max[j] = res[j];}DFS(j, end);visit[j] = 0;//res[end] +=rescue[i];}}}
}
/*
void init(){ int n,pCount; int i; scanf("%d %d %d %d",&n, &pCount, &startP, &endP); size = n; for(i=0;i<n;i++){ scanf("%d", &rescue[i]); } int r,c,value; for(i=0;i<pCount;i++){ scanf("%d %d %d", &r, &c, &value); vex[r][c] = vex[c][r] = value; }for(i=0;i<N;i++){dist[i] = 1000000;}visit[startP] = 1;  dist[startP] = 0;  path[startP] = 1;max[startP] = res[startP] = rescue[startP];DFS(startP, endP);
}
*/void init(){  int i,n,pCount;startP=0;endP=4;n=5;  size = n;  pCount=7;
//  rescue[size] = {1,2,1,5,3};  vex[0][1] = vex[1][0] = 1;  vex[0][2] = vex[2][0] = 2;  vex[0][3] = vex[3][0] = 1;  vex[1][2] = vex[2][1] = 1;vex[2][4] = vex[4][2] = 1;  vex[3][4] = vex[4][3] = 2;  for(i=0;i<N;i++){dist[i] = 1000000;}visit[startP] = 1;  dist[startP] = 0;  path[startP] = 1;max[startP] = res[startP] = rescue[startP];DFS(startP, endP);
}  int main(){  init();  //  printf("%d", vex[399][499]);  printf("%d %d", path[endP], max[endP]);return 0;
}

两个测试点没过

#include <stdio.h>  #define N 501  int rescue[N];
int res[N]={0};
int dist[N]={0};//距离值
int pred[N]={-1};//前驱
int path[N]={0};
int size=0;
int queue[N+1]={-1};
int vex[N][N]={0};
int visit[N]={0};  void updateRes(int pre){
//  printf("hello1---[%d]\n", pre);int i;if(pre != -1){for(i=0;i<size;i++){if(pred[i] == pre && i != pre){printf("hello");res[i] = res[pre] + rescue[i];updateRes(i);}}if(i == size) updateRes(-1);} else {return;}
}void BFS(int start, int end){  //队列头尾指针  int front=0,rear=0;  queue[++rear]=start;  visit[start] = 1;dist[start] = 0;path[start] = 1;res[start] = rescue[start];int i,j;  while(front!=rear){  i = queue[++front];  for(j=0;j<size;j++){  if(vex[i][j] != 0){  if(visit[j] == 0){  queue[++rear] = j;  dist[j] = dist[i] + vex[i][j];  path[j] = path[i];res[j] = res[i] + rescue[j];visit[j] = 1;  pred[j] = i;  }else { if(j!=start && j != pred[j]){  if(dist[j] == dist[i] + vex[i][j]){if(res[j] < res[i] + rescue[j]){res[j] = res[i] + rescue[j];pred[j] = i;updateRes(j);}path[j]++;}if(dist[j] > dist[i] + vex[i][j]) {  dist[j] = dist[i] + vex[i][j];  res[j] = res[i] + rescue[j];  pred[j] = i;  path[j] = 1;  updateRes(j);}    }  }  }  }  }
}  void init(){ int n,pCount,startP,endP; int i; scanf("%d %d %d %d",&n, &pCount, &startP, &endP); size = n; for(i=0;i<n;i++){ scanf("%d", &rescue[i]); } int r,c,value; for(i=0;i<pCount;i++){ scanf("%d %d %d", &r, &c, &value); vex[r][c] = vex[c][r] = value; } BFS(startP, endP); printf("%d %d", path[endP], res[endP]);
} void init(){  int n,pCount,startP=0,endP=5;  int i;  n=6;  size = n;  pCount=6;
//  rescue[size] = {1,2,1,5,3};  int r,c,value;  vex[0][1] = vex[1][0] = 1;  vex[0][2] = vex[2][0] = 3;  vex[2][3] = vex[3][2] = 1;  vex[1][2] = vex[2][1] = 1;  vex[2][4] = vex[4][2] = 1;  vex[4][5] = vex[5][4] = 1;  BFS(startP, endP);  printf("%d %d", path[endP], res[endP]);
}  int main(){  init();  return 0;
}

四个测试点没过

PAT 1003 Emergency相关推荐

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

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

  2. PAT 1003 Emergency 递归记录访问路径

    #include <stdio.h> #define N 501 #define M 1000000int rescue[N];// = {1,2,1,5,3} int startP,en ...

  3. PAT甲级1003 Emergency:[C++题解]dijkstra求最短路、最短路条数

    文章目录 题目分析 题目链接 题目分析 分析:求单源最短路,使用dijkstra()算法. 最短路的条数,和最短路中 人数最多的一条,输出最多人数. 本题点比较少,使用邻接矩阵d[N][N]来存. a ...

  4. PAT(甲级) 1003. Emergency

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

  5. PAT甲级 1003 Emergency

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

  6. PAT甲级1003 Emergency Dijkstra算法(堆优化版/朴素版)

    前言   最近花了很多的时间在写JAVA项目上面,疏忽了算法和数据结构的学习.最近突然醒悟基础更为重要,打算从今天开始每天抽出一些时间做下PAT甲级的题目.现有题库的前两题很简单,从第三题开始吧. 题 ...

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

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

  8. 1003 Emergency 题目解析

    文章目录 题目描述 题意理解 解题思路 核心算法 完整代码 题目描述 1003 Emergency As an emergency rescue team leader of a city, you ...

  9. 1003 Emergency (25 分)

    1003 Emergency (25 分) 题目大意 n个城市m条路,每个城市有救援小组,所有的边的边权已知.给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和.如果有多条就 ...

最新文章

  1. C和C++安全编码笔记:指针诡计
  2. [置顶] Linux协议栈代码阅读笔记(一)
  3. linux中sleep再循环里,Linux sleep 语句以及循环 测试负载
  4. 为什么要使用 using namespace std
  5. Array and ArrayList
  6. SAP CRM service contract和individual object
  7. linux中复制字符串出错,C语言实现字符串的复制的两种方法
  8. linux刮刮乐小游戏源代码,HTML5-Canvas实例:刮刮乐游戏
  9. 斯坦福2019强化学习课程完结,讲义、PPT、视频已提供下载
  10. C#数组和集合专题2(Array)
  11. 活动目录管理中常用的脚本(二)
  12. Eurek自我保护机制
  13. mysql now()相减_MySQL 时间函数加减计算
  14. java 答题卡_·(B卷)Java期末试卷及答题卡(2005-2006第二学期).doc
  15. 深入理解JVM - 系统性能优化
  16. 计算机温度控制系统论文,基于单片机的温度采集控制系统
  17. 计算机键盘不能用怎么办,电脑键盘空格键失灵无法使用怎么办|电脑键盘空格键失灵的解决方法...
  18. Latex排版学习笔记(1)——希腊字母表及其在latex中的表示
  19. bugku misc-旋转跳跃
  20. 字符串前加 u、r、f 的含义

热门文章

  1. 吴恩达深度学习2.3笔记_Improving Deep Neural Networks_超参数调试 和 Batch Norm
  2. Python自动化运维开发----基础(十二)函数
  3. 3星|《财经》2017年第29期:未来,国有资本的收益和变现都是补贴社保的渠道...
  4. react源码解析002 - 关于babelrc
  5. Openstack Object Store(Swift)设置公有存储的方法
  6. 番茄瑜伽13招-学好可以疏经活血祛风止痛
  7. HOHO 拿了個小嘉獎 Happy一下 可惜過年可能被留下...痛苦
  8. 冒泡排序,递归二分查找法,二分法
  9. jQuery 事件函数传参异常identifier starts immediately after numeric literal
  10. LeetCode 141 Linked List Cycle