PAT C4-GPLT L2部分题解

L2-001. 紧急救援

1.题意:

给一个点上带权,边上带长度的图,寻找最短路,要求输出所有最短路的数量并且找出一条最短路满足路上的点的权的和最大,输出最短路路径。
题目链接:https://www.patest.cn/contests/gplt/L2-001

2.思路:

最短路变形
1.在套用最短路算法的时候增加一个数组weightRes,记录走过的路节点上权重的和,每次扩充一个节点进入已确定的点集中,更新顶点的值。
2.判断的依据首先根据路途最短,在路途相等的情况下,再判断是否点上权重最大。

3.代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;

const int MAXN = 1004;
const int INF = 0x3f3f3f3f;
int N, M, S, E;
int G[MAXN][MAXN];
int dist[MAXN];
int pre[MAXN];
int nums[MAXN];
int weights[MAXN];
int weightsRes[MAXN];

bool visited[1004];
void shortestPath()
{
    memset(dist, INF, sizeof(dist));

    //visited[S] = true;
    weightsRes[S] = weights[S];
    nums[S] = 1;
    dist[S] = 0;
    for(int i = 0; i < N; i++){
        int minPath = INF;
        int u = -1;
        for(int j = 0; j < N; j++){
            if(visited[j] == false && dist[j] < minPath){
                minPath = dist[j];
                u = j;
            }
        }
        if(u == -1) break;
        visited[u] = true;
        for(int j = 0; j < N; j++){
            if(visited[j] == false && G[u][j] != INF){
                if(dist[j] > G[u][j] + dist[u]){
                    dist[j] = G[u][j] + dist[u];
                    nums[j] = nums[u];
                    pre[j] = u;
                    weightsRes[j] = weightsRes[u] + weights[j];
                }else if(dist[j] == G[u][j] + dist[u]){
                    nums[j] += nums[u];
                    if(weightsRes[u] + weights[j] > weightsRes[j]){
                        weightsRes[j] = weightsRes[u] + weights[j];
                        pre[j] = u;
                    }
                }
            }
        }
    }

}

void initG()
{
    memset(G, INF, sizeof(G));
    for(int i = 0; i < N; i++){
        G[i][i] = 0;
    }
    for(int i = 0; i < N; i++){
        pre[i] = -1;
    }
}

int main()
{
    //printf("%d\n", gcd(1, 12));

    //freopen("in.txt", "r", stdin);
    scanf("%d %d %d %d", &N, &M, &S, &E);
    initG();

    for(int i = 0; i < N; i++){
        scanf("%d", &weights[i]);
    }

    for(int i = 0; i < M; i++){
        int u, v, l;
        scanf("%d %d %d", &u, &v, &l);
        G[u][v] = l;
        G[v][u] = l;
    }

    shortestPath();


//    for(int i = 0; i < N; i++){
//        printf("%d ", nums[i]);
//    }
//    printf("%d\n", G[5][5]);

    printf("%d %d\n", nums[E], weightsRes[E]);
    vector<int> res;
    int p = E;
    while(pre[p] != -1){
        res.push_back(p);
        p = pre[p];
    }
    res.push_back(S);
    int len = res.size();
    for(int i = len-1; i > 0; i--){
        printf("%d ", res[i]);
    }
    printf("%d\n", res[0]);

    return 0;
}

L2-002. 链表去重

1.题意:

给一个链表,要求去除其键值的绝对值有重复的结点
题目链接:https://www.patest.cn/contests/gplt/L2-002

2.思路:

只需要建立一个数组标记key的绝对值,发现有重复的时候,对应地址标上删除标记,然后新建2个地址表,一个存放不被删的,一个存放被删的。扫两遍就完了。(发现这些题都没什么思维难度,主要是实现麻烦

3.代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;

int mp[1000010];
int head;
int next0[1000010];
int n;
bool exist[11000];
bool del[1000010];

int next1[1000010];
int next2[1000010];

int main()
{
    //freopen("in.txt", "r", stdin);

    scanf("%d %d", &head, &n);

    for(int i = 0; i < n; i++){
        int add, key, ne;
        scanf("%d %d %d", &add, &key, &ne);
        mp[add] = key;
        next0[add] = ne;
    }
    int p = head;
    while(p != -1){
        int key = abs(mp[p]);
        if(false == exist[key]){
            exist[key] = true;
        }else{
            del[p] = true;
        }
        //printf("%d ", key);
        p = next0[p];
    }

    p = next0[head];
    int preAdd = head;
    while(p != -1){
        if(del[p] == false){
            next1[preAdd] = p;
            preAdd = p;
        }
        p = next0[p];
    }
    next1[preAdd] = -1;

    p = next0[head];
    preAdd = head;
    while(p != -1){
        if(del[p] == true){
            next2[preAdd] = p;
            preAdd = p;
        }
        p = next0[p];
    }
    next2[preAdd] = -1;

    p = head;
    while(p!=-1){
        if(next1[p] != -1)
            printf("%05d %d %05d\n", p, mp[p], next1[p]);
        else{
            printf("%05d %d %d\n", p, mp[p], next1[p]);
        }
        p = next1[p];
    }

    p = next2[head];
    while(p!=-1){
        if(next2[p] != -1)
            printf("%05d %d %05d\n", p, mp[p], next2[p]);
        else{
            printf("%05d %d %d\n", p, mp[p], next2[p]);
        }
        p = next2[p];
    }
    return 0;
}

L2-003. 月饼

1.题意:

https://www.patest.cn/contests/gplt/L2-003

2.思路:

排个序就完了,单位价值多的先卖,毕竟可以卖部分,不存在背包那些问题。

3.代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;

int N;
double sum;
struct Node
{
    double money;
    double n;
};

Node nodes[1314];

bool cmp(Node p1, Node p2)
{
    return p1.money/p1.n > p2.money/p2.n;
}

int main()
{
   // freopen("in.txt", "r", stdin);

    scanf("%d %lf", &N, &sum);

    for(int i = 0; i < N; i++){
        scanf("%lf", &nodes[i].n);
    }

    for(int i = 0; i < N; i++){
        scanf("%lf", &nodes[i].money);
    }

    sort(nodes, nodes+N, cmp);

    int i;
    double cnt = 0;
    double res = 0.;
    for(i = 0; i < N; i++){
        //printf("%lf ", nodes[i].money);
        cnt += nodes[i].n;
        if(cnt >= sum){
            break;
        }
        res += nodes[i].money;
    }
    if(i != N){
        cnt -= nodes[i].n;
        //printf("%lf ", cnt);
        //printf("%.2lf\n", res);
        //i--;
        res+=nodes[i].money/nodes[i].n*(sum-cnt);
    }
    printf("%.2lf\n", res);
    return 0;
}

L2-005. 集合相似度

1.题意:

https://www.patest.cn/contests/gplt/L2-005

2.思路:

集合的交除以集合的并,stl练习题,java居然不给过

3.代码

#include <set>
#include <vector>
#include <cstdio>
using namespace std;
int main() {
    int n, m, k, temp, a, b;
    scanf("%d", &n);
    vector<set<int>> v(n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &m);
        set<int> s;
        for(int j = 0; j < m; j++) {
            scanf("%d", &temp);
            s.insert(temp);
        }
        v[i] = s;
    }
    scanf("%d", &k);
    for(int i = 0; i < k; i++) {
        scanf("%d %d", &a, &b);
        int nc = 0, nt = v[b-1].size();
        for(auto it = v[a-1].begin(); it != v[a-1].end(); it++) {
            if(v[b-1].find(*it) == v[b-1].end()) {
                nt++;
            } else {
                nc++;
            }
        }
        double ans = (double)nc / nt * 100;
        printf("%.2f%%\n", ans);
    }
    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页