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












第一行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至少有一条路径。
一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
3 2 0 2 1 2 3 0 1 10 1 2 11
21 6
大佬的代码
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 550;
int co[MAX], dist[MAX], g[MAX][MAX],low[MAX];
int n, m, s, e;
bool vis[MAX];
void dijistra(){
for(int i = 0; i < n; i ++){
dist[i] = INF;
}
memset(vis,0,sizeof(vis));
memset(low,0,sizeof(vis));
dist[s] = 0;
low[s] = co[s];
for(int i = 1; i <= n; i ++){
int mins = INF, MAx = 0, pos;
for(int j = 0; j < n; j ++){
if(!vis[j] && dist[j] < mins){
pos = j;
mins = dist[j];
MAx = co[j];
}
if(!vis[j] && dist[j] == mins && MAx < low[j]){
pos = j;
MAx = low[j];
}
}
if(mins == INF)break;
vis[pos] = true;
for(int j = 1; j <= n; j ++){
if(!vis[j] && dist[j] > dist[pos] + g[pos][j]){
dist[j] = dist[pos] + g[pos][j];
low[j] = low[pos] + co[j];
}
if(!vis[j] && low[j] < low[pos] + co[j] && dist[j] == dist[pos]+g[pos][j]){
low[j] = low[pos] + co[j];
}
}
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&e);
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j ++)
g[i][j] = (i==j)?0:INF;
}
for(int i = 0; i < n; i ++) scanf("%d",&co[i]);
for(int i = 0; i < m; i ++) {
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
if(g[u][v] > w) {
g[u][v] = g[v][u] = w;
}
}
dijistra();
printf("%d %d\n",dist[e],low[e]);
return 0;
}
标签:math color letter 起点 back 运行 描述 科学 ==
原文地址:http://www.cnblogs.com/ruruozhenhao/p/7502605.html