C++PTA A1087(dijkstra+dfs)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

这里有许多不同的游客从城市到罗马,要求你找出你的最大希望游览的路线最小成本。

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

输入规格:每个输入文件包含一个测试样例,针对每个测试样例,第一行包含两个正整数N,城市的数量,两个城市之间总的路线数,紧接着,根据起始城市的名字,下和他的期望值,然后K行紧接着,每个描述路线这样的格式,一个名字,三个首字母长度,和一个目的地,花费

Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->…->ROM.

输出要求:针对每个测试样例,要求你找到最小话费,如果这样的路径不唯一,那就推荐最大的期望度,如果这样的路径不唯一,那就输出最大的期望度,判断保证这是唯一的。因此在第一行输出,你必须打印四个数字最小的花费陈你根本,花费,期望度,平均期望度,推荐的路线,在下一行要求你打印城市1城市2 —》Rom

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

核心思路

dijkstra算法+dfs

完整源码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
const int MAXV = 210;
const int INF = 1<< 30;

//n为定点数,m为边数,st为起点,G为领接矩阵,weight为点权
//d[]记录最短距离,w[]记录最大点权 num[]记录最短路径条数
//pt[]记录1最短路径上的定点数,pre[]记录前驱
int n,m,st,G[MAXV][MAXV],weight[MAXV];
int d[MAXV],w[MAXV],num[MAXV],pt[MAXV],pre[MAXV];
bool vis[MAXV] = {false};//vis[i] == true 表示顶点i已访问,初值均为false
map<string,int>cityToIndex; //将城市名转化为编号
map<int,string>indexToCity; //将编号转化为城市名

void Dijkstra(int s) {
    fill(d, d + MAXV, INF);
    memset(w, 0, sizeof(w));
    memset(num, 0, sizeof(num));
    memset(pt, 0, sizeof(pt));
    for (int i = 0; i < n; i++) pre[i] = i;
    d[s] = 0;
    w[s] = weight[st];
    num[s] = 1;
    for (int i = 0; i < n; i++) {//循环n次
        int u = -1, MIN = INF;//u使d[u]最小,MIN存放该最小的d[u]
        for (int j = 0; j < n; j++) {
            if (vis[j] == false && d[j] < MIN) {
                u = j;
                MIN = d[j];
            }
        }
        //找不到iNF
        if (u == -1) return;
        vis[u] = true; //标记u为已访问
        for (int v = 0; v < n; v++) {
            //如果v未访问 && u能到达v
            if (vis[v] == false && G[u][v] != INF) {
                if (d[u] + G[u][v] < d[v]) {
                    d[v] = d[u] + G[u][v]; //优化d[v]
                    w[v] = w[u] + weight[v]; //覆盖w[v]
                    num[v] = num[u];
                    pt[v] = pt[u] + 1; //p->v顶点个数等于s->u顶点个数+1
                    pre[v] = u;
                } else if (d[u] + G[u][v] == d[v]) { //找到相同长度的路径
                    num[v] += num[u]; //到v的最短路径条数继承自num[u]
                    if (w[u] + weight[v] > w[v]) {
                        w[v] = w[u] + weight[v]; //优化w[v]
                        pt[v] = pt[u] + 1;//s->v顶点个数等于s->u顶点个数
                        pre[v] = u; //v的前驱为u
                    } else if (w[u] + weight[v] == w[v]) {
                        //找到相同的点权之和的路径
                        double uAvg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1);
                        double vAvg = 1.0 * w[v] / pt[v];
                        if (uAvg > vAvg) {
                            pt[v] = pt[u] + 1;
                            pre[v] = u;
                        }
                    }
                }
            }
        }
    }
}
void printPath(int v){
    if(v == 0){
        cout << indexToCity[v];
        return ;
    }
    printPath(pre[v]);
    cout << "->" << indexToCity[v];
}
int main()
{
    string start,city1,city2; //start为起始城市名
    cin >> n >> m >> start; //读入城市数,边数及起始城市
    cityToIndex[start] = 0;//起始城市下标记住为0
    indexToCity[0] = start;//下标0对应起始城市的名称
    for(int i = 1;i<= n-1;i++){
        cin >> city1 >> weight[i];//读入城市名称city与其点权
        cityToIndex[city1] = i; //城市1city1下标记为i
        indexToCity[i] = city1;//下标i对应1城市city的名称
    }
    fill(G[0],G[0]+MAXV*MAXV,INF);//初始化图G
    for(int i =0;i<m;i++){
        cin >> city1 >> city2; //边的两个断点
        int c1 = cityToIndex[city1],c2 = cityToIndex[city2];
        cin >> G[c1][c2]; //边权1
        G[c2][c1] = G[c1][c2];//无向边
    }
    Dijkstra(0);//算法入口
    int rom = cityToIndex["ROM"];
    printf("%d %d %d %d\n",num[rom],d[rom],w[rom],w[rom]/pt[rom]);
    printPath(rom);
    return 0;
}

方法2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXV = 210;
const int INF = 1<< 30;

//n为定点数,m为边数,st为起点,G为领接矩阵,weight为点权
//d[]记录最短距离,w[]记录最大点权 num[]记录最短路径条数
//pt[]记录1最短路径上的定点数,pre[]记录前驱
int n,m,st,G[MAXV][MAXV],weight[MAXV];
int d[MAXV],numPath = 0,maxW = 0;
double maxAvg = 0;
bool vis[MAXV] = {false}; //vis[i] == true 表示顶点i已访问,初始均为false
vector<int> pre[MAXV]; //前驱
vector<int> tempPath,path;//临时路径及最优路径
map<string,int>cityToIndex; //将城市名转化为编号
map<int,string>indexToCity; //将编号转换为城市名

void Dijkstra(int s) {
    fill(d, d + MAXV, INF);
    d[s] = 0;
    for (int i = 0; i < n; i++) {//循环n次
        int u = -1, MIN = INF;//u使d[u]最小,MIN存放该最小的d[u]
        for (int j = 0; j < n; j++) {
            if (vis[j] == false && d[j] < MIN) {
                u = j;
                MIN = d[j];
            }
        }
        //找不到iNF
        if (u == -1) return;
        vis[u] = true; //标记u为已访问
        for (int v = 0; v < n; v++) {
            //如果v未访问 && u能到达v
            if (vis[v] == false && G[u][v] != INF) {
                if (d[u] + G[u][v] < d[v]) {
                    d[v] = d[u] + G[u][v]; //优化d[v]
                    pre[v].clear();       //清空pre[v]
                    pre[v].push_back(u); //u为v的前驱
                } else if (d[u] + G[u][v] == d[v]) { //找到相同长度的路径
                    pre[v].push_back(u);
                }
            }
        }
    }
}

void DFS(int v){
    if(v== st){
        tempPath.push_back(v);
        numPath++;
        int tempW = 0;
        for(int i = tempPath.size()-2;i>=0;i--){
            int id = tempPath[i]; //当前结点
            tempW += weight[id];  //增加点id的点权(注意不是i)
        }
        double tempAvg = 1.0 * tempW / (tempPath.size()-1);
        if(tempW > maxW) {
            maxW = tempW; //优化maxW
            maxAvg = tempAvg; //覆盖maxAvg
            path = tempPath; //覆盖path
        }else if(tempW == maxW && tempAvg > maxAvg){
            maxAvg = tempAvg; //优化maxAvg
            path = tempPath; //覆盖path
        }
        tempPath.pop_back();
        return ;
    }
    tempPath.push_back(v);
    for(int i =0;i<pre[v].size();i++){
        DFS(pre[v][i]);
    }
    tempPath.pop_back();
}

int main()
{
    string start,city1,city2; //start为起始城市名
    cin >> n >> m >> start; //读入城市数,边数及起始城市
    cityToIndex[start] = 0;//起始城市下标记住为0
    indexToCity[0] = start;//下标0对应起始城市的名称
    for(int i = 1;i<= n-1;i++){
        cin >> city1 >> weight[i];//读入城市名称city与其点权
        cityToIndex[city1] = i; //城市1city1下标记为i
        indexToCity[i] = city1;//下标i对应1城市city的名称
    }
    fill(G[0],G[0]+MAXV*MAXV,INF);//初始化图G
    for(int i =0;i<m;i++){
        cin >> city1 >> city2; //边的两个断点
        int c1 = cityToIndex[city1],c2 = cityToIndex[city2];
        cin >> G[c1][c2]; //边权1
        G[c2][c1] = G[c1][c2];//无向边
    }
    Dijkstra(0);//算法入口
    int rom = cityToIndex["ROM"];
    DFS(rom);
    printf("%d %d %d %d\n",numPath,d[rom],maxW,(int)maxAvg);
    for(int i =path.size()- 1;i>=0;i--){
        cout << indexToCity[path[i]];
        if(i>0) cout << "->";
    }
    return 0;
}
上一篇:LeetCode学习-第二十七天


下一篇:BUUCTF:[UTCTF2020]sstv