PAT1003 Emergency (25 分)
阿新 • • 發佈:2018-12-16
解析
第一份程式碼:Dijkstra演算法。
第二份程式碼:Dijkstra演算法+DFS
詳情見:PAT && 圖總結。
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1000;
const int INF = 0x3fffffff;
struct Node
{
int v, w;
Node(int _v, int _w) :v(_v), w(_w) { ; }
};
vector< vector<Node>> G;
int weight[1000]{ 0 };
vector<int> d, gather,num;
void dijksta(int s) {
for (int i = 0; i < G.size(); i++) {
d[i] = INF;
gather[i] = 0;
num[i] = 0;
}
d[s] = 0;
gather[s] = weight[s];
num[s] = 1;
vector<bool> vis(G.size(), false);
for (int i = 0; i < G. size(); i++) {
int u = -1, min = INF;
for (int j = 0; j < G.size(); j++) { //找出最小
if (vis[j] == false && d[j] < min) {
u = j;
min = d[j];
}
}
vis[u] = true;
for (auto x : G[u]) { //對於連線頂點u的邊
if (vis[x.v] == false) {
if (d[u] + x.w < d[x.v]) {
d[x.v] = d[u] + x.w;
gather[x.v] = gather[u] + weight[x.v];
num[x.v] = num[u];
}
else if (d[u] + x.w == d[x.v]){
if(gather[u]+weight[x.v] > gather[x.v])
gather[x.v] = gather[u] + weight[x.v];
num[x.v] += num[u];
}
}
}
}
}
int main()
{
int N, M, city_in, city_save;
scanf("%d %d %d %d", &N, &M, &city_in, &city_save);
G.resize(N),d.resize(N),gather.resize(N),num.resize(N);
for (int i = 0; i < N; i++)
scanf("%d", &weight[i]);
int city1, city2, len;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &city1, &city2, &len);
G[city1].push_back(Node(city2, len));
G[city2].push_back(Node(city1, len));
}
dijksta(city_in);
printf("%d %d\n", num[city_save], gather[city_save]);
}
/*
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
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
const int INF = 0x3fffffff;
vector<vector<pair<int,int>>> G;
vector<int> weight;
vector<vector<int>> pre;
int num = 0; //最短路徑條數
int optvalue = 0;
vector<int> temppath;
void Dijksta(int s,vector<int>& d) {
vector<bool> vis(G.size(), false);
d.resize(G.size(),INF);
pre.resize(G.size());
d[s] = 0;
for (int i = 0; i < G.size(); i++) {
int u = -1, min = INF;
for (int j = 0; j < G.size(); j++) {
if (vis[j] == false && min > d[j]) {
u = j;
min = d[j];
}
}
vis[u] = true;
for (auto x : G[u]) {
int v = x.first;
if (vis[v] == false) {
if (d[v] > d[u] + x.second) {
pre[v].clear();
pre[v].push_back(u);
d[v] = d[u] + x.second;
}
else if (d[v] == d[u] + x.second) {
pre[v].push_back(u);
}
}
}
}
}
void DFS(int s,int v) {
if (s == v) {
temppath.push_back(s);
int tempValue = 0;
for (auto x : temppath)
tempValue += weight[x];
if (optvalue < tempValue)
optvalue = tempValue;
temppath.pop_back();
num++;
return;
}
temppath.push_back(v);
for (auto x : pre[v])
DFS(s, x);
temppath.pop_back();
}
int main()
{
int N, M, city_in, city_save;
scanf("%d %d %d %d", &N, &M, &city_in, &city_save);
G.resize(N);
weight.resize(N);
for (int i = 0; i < N; i++)
scanf("%d", &weight[i]);
int city1, city2, len;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &city1, &city2, &len);
G[city1].push_back(make_pair(city2, len));
G[city2].push_back(make_pair(city1, len));
}
vector<int> d;
Dijksta(city_in,d);
DFS(city_in,city_save);
printf("%d %d", num, optvalue);
}