栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【pat】1003 Emergency 关于测试用例2、3、4的一点想法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【pat】1003 Emergency 关于测试用例2、3、4的一点想法

1003 Emergency

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.


基本用例:
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

输出:

2 4
其他2:

测试点2、4

5 6 0 4
1 2 1 5 3
0 1 1
0 2 2
0 3 2
1 2 1
2 4 1
3 4 1

输出

3 9
其他3:

测试点2、4

5 6 0 4
1 1 1 1 1
0 1 1
0 2 2
0 3 2
1 2 1
2 4 1
3 4 1

输出:

3 4

其他4:

输入:

5 4 0 4
1 1 1 1 1 
0 2 2
0 3 1
2 3 1
2 4 1

输出:

2 4
实现代码:

100%pass

#include 
#include

using namespace std;
struct Path {
    int end;
    int length;
    Path(int end, int length) :end(end), length(length) {}
};


void getPathsAndMinLength(int start, int end, int citys, vector* graph, vector* teams) {
    bool* visited = new bool[citys];
    //设置为未访问
    for (int i = 0; i < citys; i++)
    {
        visited[i] = false;
    }
    // 初始化当前路径
    int* pathLength = new int[citys];
    vector* pre = new vector[citys];
    int* weight = new int[citys];
    int* num = new int[citys];
    for (int i = 0; i < citys; i++)
    {
        if (i == start)
        {
            pathLength[i] = 0;
            num[i] = 1;
            pre[i].push_back(start);
            weight[i] = (*teams)[i];
        }
        else {
            pathLength[i] = 0x3FFFFFFF;
            num[i] = 0;
            weight[i] = 0;
        }
    }
    for (int i = 0; i < graph[start].size(); i++)
    {
        pathLength[graph[start][i].end] = graph[start][i].length;
        pre[graph[start][i].end].push_back(start);
        weight[graph[start][i].end] = weight[start] + (*teams)[graph[start][i].end];
        num[graph[start][i].end] = num[start];
    }
    bool hasUnvisted = true;
    visited[start] = true;
    while (hasUnvisted)
    {
        hasUnvisted = false;
        if (visited[end] == true)
        {
            break;
        }
        // 选取当前最短的路径访问
        int minPath = 0x3FFFFFFF, nextNode = 0;
        for (int i = 0; i < citys; i++)
        {
            //未访问过而且当前到原点start的路径最短
            if (visited[i] == false && minPath > pathLength[i]) {
                minPath = pathLength[i];
                nextNode = i;
                hasUnvisted = true;
            }
        }
        if (hasUnvisted)
        {
            visited[nextNode] = true;
            // 如果还有没有访问,但是可以访问的点,则以这个点为中间点继续访问
            vector path = graph[nextNode];
            for (int i = 0; i < path.size(); i++)
            {
                // 如果没有访问过,
                if (visited[path[i].end] == false && path[i].length < 0x3FFFFFFF) {
                    if (pathLength[nextNode] + path[i].length < pathLength[path[i].end])
                    {
                        pathLength[path[i].end] = pathLength[nextNode] + path[i].length;
                        pre[path[i].end].clear();
                        pre[path[i].end].push_back(nextNode);
                        num[path[i].end] = num[nextNode];
                        weight[path[i].end] = weight[nextNode] + (*teams)[path[i].end];
                    }
                    else if (pathLength[nextNode] + path[i].length == pathLength[path[i].end])
                    {
                        // 遇到了相同长度的路径,经过更多的中间节点的救援队更好
                        if (weight[nextNode] + (*teams)[path[i].end] > weight[path[i].end])
                        {
                            weight[path[i].end] = weight[nextNode] + (*teams)[path[i].end];
                        }
                        num[path[i].end] += num[nextNode];
                        pre[path[i].end].push_back(nextNode);
                    }
                }
            }
        }
    }
    cout << num[end] << " " << weight[end] << endl;
}


int main()
{
    int citys, roads, now, toCity;
    cin >> citys >> roads >> now >> toCity;
    vector teams;
    int team;
    for (int i = 0; i < citys; i++)
    {
        cin >> team;
        teams.push_back(team);
    }
    vector* graph = new vector[citys];
    int start, end, length;
    for (int i = 0; i < roads; i++)
    {
        cin >> start >> end >> length;
        graph[start].push_back(Path(end, length));
        graph[end].push_back(Path(start, length));
    }
    getPathsAndMinLength(now, toCity, citys, graph, &teams);

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889714.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号