51nod 1459 迷宫游戏

你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?

Input
第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。
第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。
再接下来m行,每行3个空格分隔的整数x, y, z (0<z<=200)表示道路,表示从房间x到房间y(双向)的道路,注意,最多只有一条道路连结两个房间, 你需要的时间为z。
输入保证从start到end至少有一条路径。
Output
一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
Input示例
3 2 0 2
1 2 3
0 1 10
1 2 11
Output示例
21 6

求最短路径,不过加了一个分数,在最短路径上求最大分数。用dijistra算法算最短路径,不过在找相邻最短路径时,如果路径相同,则取分数最大的点。
 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 const int MAX = 550;
 8 int co[MAX], dist[MAX], g[MAX][MAX],low[MAX];
 9 int n, m, s, e;
10 bool vis[MAX];
11 void dijistra(){
12     for(int i = 0; i < n; i ++){
13         dist[i] = INF;
14     }
15     memset(vis,0,sizeof(vis));
16     memset(low,0,sizeof(vis));
17     dist[s] = 0;
18     low[s] = co[s];
19     for(int i = 1; i <= n; i ++){
20         int mins = INF, MAx = 0, pos;
21         for(int j = 0; j < n; j ++){
22             if(!vis[j] && dist[j] < mins){
23                 pos = j;
24                 mins = dist[j];
25                 MAx = co[j];
26             }
27             if(!vis[j] && dist[j] == mins && MAx < low[j]){
28                 pos = j;
29                 MAx = low[j];
30             }
31         }
32         if(mins == INF)break;
33         vis[pos] = true;
34         for(int j = 1; j <= n; j ++){
35             if(!vis[j] && dist[j] > dist[pos] + g[pos][j]){
36                 dist[j] = dist[pos] + g[pos][j];
37                 low[j] = low[pos] + co[j];
38             }
39             if(!vis[j] && low[j] < low[pos] + co[j] && dist[j] == dist[pos]+g[pos][j]){
40                 low[j] = low[pos] + co[j];
41             }
42         }
43     }
44 }
45 int main(){
46     scanf("%d%d%d%d",&n,&m,&s,&e);
47     for(int i = 0; i < n; i ++){
48         for(int j = 0; j < n; j ++)
49             g[i][j] = (i==j)?0:INF;
50     }
51     for(int i = 0; i < n; i ++) scanf("%d",&co[i]);
52     for(int i = 0; i < m; i ++) {
53         int u, v, w;
54         scanf("%d%d%d",&u,&v,&w);
55         if(g[u][v] > w) {
56             g[u][v] = g[v][u] = w;
57         }
58     }
59     dijistra();
60     printf("%d %d
",dist[e],low[e]);
61     return 0;
62 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/7243210.html