problem

1003 Emergency (25)(25 point(s))
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

anwser

Dijkstra 解法
#include<bits/stdc++.h> #define INF 0x3f3f3f3f
#define Max 511 int N, M, C1, C2;
int Rescue[Max], Map[Max][Max], Dis[Max], Pre[Max], W[Max], Diff[Max];
bool Vis[Max] = {false}; void Dijkstra(int s){
memset(Dis, INF, sizeof(Dis));
memset(W, 0, sizeof(W));
memset(Diff, 0, sizeof(Diff)); Dis[s] = 0;
W[s] = Rescue[s];
Diff[s] = 1;
for(int i = 0; i < N; i++) Pre[i] = i; for(int i = 0; i < N; i++){
int u = 0, minn = INF;
for(int j = 0; j < N; j++){
if(!Vis[j] && Dis[j] < minn){
u = j;
minn = Dis[j];
}
} if(u == C2 || minn == INF) return;
Vis[u] = true; for(int v = 0; v < N; v++) {
if(!Vis[v]) {
if(Dis[u] + Map[u][v] < Dis[v]){
Dis[v] = Dis[u] + Map[u][v];
// Pre[v] = u;
// }
W[v] = W[u] + Rescue[v];
Diff[v] = Diff[u];
}else if (Dis[u] + Map[u][v] == Dis[v]){
Diff[v] += Diff[u];
if(W[u] + Rescue[v] > W[v]){
W[v] = W[u] + Rescue[v];
// Pre[v] = u;
}
} }
}
}
} int main(){
// freopen("test.txt", "r", stdin); memset(Map, INF, sizeof(Map)); std::cin>>N>>M>>C1>>C2;
for(int i = 0; i < N; i++){
std::cin>>Rescue[i];
} for(int i = 0; i < M; i++){
int c1, c2, L;
std::cin>>c1>>c2>>L;
Map[c1][c2] = Map[c2][c1] = L;
} Dijkstra(C1); std::cout<<Diff[C2]<<" "<<W[C2]; return 0;
} /*
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
*/
DFS解法

#include<bits/stdc++.h>
#include<vector> #define INF 0x3f3f3f3f
#define Max 511 int N, M, C1, C2;
int Rescue[Max], Map[Max][Max], Dis[Max], Pre[Max], W[Max], Diff[Max];
bool Vis[Max] = {false}; void Dijkstra(int s){
memset(Dis, INF, sizeof(Dis));
memset(W, 0, sizeof(W));
memset(Diff, 0, sizeof(Diff)); Dis[s] = 0;
W[s] = Rescue[s];
Diff[s] = 1;
for(int i = 0; i < N; i++) Pre[i] = i; for(int i = 0; i < N; i++){
int u = 0, minn = INF;
for(int j = 0; j < N; j++){
if(!Vis[j] && Dis[j] < minn){
u = j;
minn = Dis[j];
}
} if(u == C2 || minn == INF) return;
Vis[u] = true; for(int v = 0; v < N; v++) {
if(!Vis[v]) {
if(Dis[u] + Map[u][v] < Dis[v]){
Dis[v] = Dis[u] + Map[u][v];
W[v] = W[u] + Rescue[v];
Diff[v] = Diff[u];
}else if (Dis[u] + Map[u][v] == Dis[v]){
Diff[v] += Diff[u];
if(W[u] + Rescue[v] > W[v]){
W[v] = W[u] + Rescue[v];
}
} }
}
}
} int minDis = INF, diff = 0, maxTeam = 0, vis[Max]; void DFS(int v, int dis, int team){
if(v == C2){
if(dis < minDis)
{
minDis = dis;
diff = 1;
maxTeam = team;
}else if(dis == minDis){
diff++;
if(team > maxTeam) maxTeam = team;
}
// std::cout<<team<<std::endl;
return ;
}
vis[v] = 1;
for(int i = 0; i < N; i++)
if(vis[i] == 0 && Map[v][i] != INF)
DFS(i, dis + Map[v][i], team + Rescue[i]);
vis[v] = 0;
} int main(){
// freopen("test.txt", "r", stdin); memset(Map, INF, sizeof(Map));
memset(vis, 0, sizeof(vis)); std::cin>>N>>M>>C1>>C2;
for(int i = 0; i < N; i++){
std::cin>>Rescue[i];
} for(int i = 0; i < M; i++){
int c1, c2, L;
std::cin>>c1>>c2>>L;
Map[c1][c2] = Map[c2][c1] = L;
} // Dijkstra(C1);
// std::cout<<Diff[C2]<<" "<<W[C2]; DFS(C1, 0, Rescue[C1]);
std::cout<<diff<<" "<<maxTeam; return 0;
} /*
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
*/

experience

  • 注意审题,求的不是最短路,是最短路的不同路条数。
  • 这个图不是单向图,是双向图。
  • Dijkstra算法以及其变种需要熟悉。

    单词复习
  • scattered 分散的

1003 Emergency (25)(25 point(s))的更多相关文章

  1. MySQL5.7.25(解压版)Windows下详细的安装过程

    大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...

  2. PAT 甲级 1006 Sign In and Sign Out (25)(25 分)

    1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...

  3. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  4. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  5. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

  6. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  7. 【PAT】1015 德才论 (25)(25 分)

    1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得 ...

  8. 1002 A+B for Polynomials (25)(25 point(s))

    problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...

  9. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

随机推荐

  1. HDU1505 City Game 悬线法

    题意: 给出一个像这样的矩阵 R F F F F F F F F F F F R R R F F F     F F F F F F F F F F F F 求F组成的最大子矩阵(面积最大) 有多组数 ...

  2. 【疑点】<p></p>标签为什么不能包含块级标签?还有哪些特殊的HTML标签?

    最近,在码代码的时候,就是下面的这段代码,我犯了一个很不起眼,但犯了就致命的BUG. <body> <p> <ol> <li>Hello</li& ...

  3. Openflow Plugin学习笔记3

    MDController.java 中的start方法,创建了SwitchConnectionHandlerImpl实例 SwitchConnectionHandlerImpl switchConne ...

  4. rollup&&cube

    group by 擴展 rollup&&cube --按job分組計算不同job的匯總工資   SELECT job, SUM (sal)     FROM emp GROUP BY ...

  5. mysql使用模板解决旧数据处理,默认初始化数据的通用方法!

    一 业务介绍 先来看看我这得大致业务需求,这次业务比较简单: 即从现在开始,每次new一个爷爷都需要默认初始化给这个爷爷三个儿子(子表,爷爷id去关联),并在初始化每个儿子的同时再给每个儿子初始化若干 ...

  6. mysql -> 索引_07

    索引与sql语句优化 压力测试对比

  7. 将python脚本转换成exe文件--pyinstaller

    遇到的大坑: 直接运行python文件效果:         执行 pyinstaller  -F -w  -p  -i ./123.ico  ./main.py    在dict文件夹下生成exe文 ...

  8. scala中“_”的用法

    参见链接 http://blog.csdn.net/i6448038/article/details/50017427

  9. TF-图像的深度和通道的概念(转)

    图像的深度和通道概念 图像的深度: 图片是由一个个像素点构成的,所有不同颜色的像素点构成了一副完整的图像,计算机存储图片是以二进制来进行的. 1 bit : 用一位来存储,那么这个像素点的取值范围就是 ...

  10. 读书笔记--C陷阱与缺陷(六)

    第六章 1.预处理器:预处理器先对代码进行必要的转换处理,简化编程者的工作. 它的重要原因有以下两点: a. 假如要将程序中出现的所有实例都加以修改,但希望只改动程序一处数值,重新编译实现. 预处理器 ...