CCCC-L3-011. 直捣黄龙

L3-011. 直捣黄龙

时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。

输入格式:

输入第一行给出2个正整数N(2 <= N <= 200,城镇总数)和K(城镇间道路条数),以及己方大本营和敌方大本营的代号。随后N-1行,每行给出除了己方大本营外的一个城镇的代号和驻守的敌军数量,其间以空格分隔。再后面有K行,每行按格式“城镇1 城镇2 距离”给出两个城镇之间道路的长度。这里设每个城镇(包括双方大本营)的代号是由3个大写英文字母组成的字符串。

输出格式:

按照题目要求找到最合适的进攻路径(题目保证速度最快、解放最多、杀伤最强的路径是唯一的),并在第一行按照格式“己方大本营->城镇1->...->敌方大本营”输出。第二行顺序输出最快进攻路径的条数、最短进攻距离、歼敌总数,其间以1个空格分隔,行首尾不得有多余空格。

输入样例:
10 12 PAT DBY
DBY 100
PTA 20
PDS 90
PMS 40
TAP 50
ATP 200
LNN 80
LAO 30
LON 70
PAT PTA 10
PAT PMS 10
PAT ATP 20
PAT LNN 10
LNN LAO 10
LAO LON 10
LON DBY 10
PMS TAP 10
TAP DBY 10
DBY PDS 10
PDS PTA 10
DBY ATP 10
输出样例:
PAT->PTA->PDS->DBY
3 30 210

考查多标尺最短路,并打印路径~

1.原生Dijkstra能过,利用dfs或栈或数组记录路径

2.将节点字符串名字映射成数字编号,可以使用map

3.剩下就是跑最短路了~

#include <iostream>
#include <string>
#include <vector>
#include <map>

#define MAX 200 + 10
#define INF 0x3fffffff

using namespace std;

map<string, int> mpId;
map<int, string> mpString;

int Queue[MAX];
int index = 0;

int MGraph[MAX][MAX];
int dist[MAX];
int visit[MAX];

vector<int> path[MAX];
vector<int> tempPath;
vector<int> bestPath;
int cntCity = 0;
int cntCost = 0;
int cntPath = 0;

void Dijkstra( int n ) {
    fill( dist, dist + MAX, INF );
    dist[0] = 0;
    for( int i = 0; i < n; i++ ) {
        int u = -1, MIN = INF;
        for( int j = 0; j < n; j++ ) {
            if( !visit[j] && dist[j] < MIN ) {
                u = j;
                MIN = dist[j];
            }
        }
        if( u == -1 ) return;
        visit[u] = 1;
        for( int v = 0; v < n; v++ ) {
            if( !visit[v] && MGraph[u][v] != 0 ) {
                if( MGraph[u][v] + dist[u] < dist[v] ) {
                    dist[v] = MGraph[u][v] + dist[u];
                    path[v].clear();
                    path[v].push_back( u );
                }
                else if( MGraph[u][v] + dist[u] == dist[v] ) {
                    path[v].push_back( u );
                }
            }
        }
    }
}

void dfs( int v, int s ) {
    if( v == s ) {
        cntPath++;
        tempPath.push_back( s );
        int sum = 0;
        for( int i = 0; i < tempPath.size(); i++ ) {
            sum = sum + Queue[tempPath[i]];
        }
        if( tempPath.size() > cntCity ) {
            cntCity = tempPath.size();
            bestPath = tempPath;
            cntCost = sum;
        }
        else if( tempPath.size() == cntCity ) {
            if( sum > cntCost ) {
                bestPath = tempPath;
                cntCost = sum;
            }
        }

        tempPath.pop_back();
        return;
    }
    tempPath.push_back( v );
    for( int i = 0; i < path[v].size(); i++ ) {
        dfs( path[v][i], s );
    }
    tempPath.pop_back();
}

int main() {
    int n, k;
    int endId;
    string start, end;
    scanf( "%d%d", &n, &k );
    cin >> start >> end;
    mpId[start] = index;
    mpString[index++] = start;

    for( int i = 0; i < n - 1; i++ ) {
        string city;
        int army;
        cin >> city >> army;
        Queue[index] = army;
        if( city == end ) endId = index;
        mpId[city] = index;
        mpString[index++] = city;
    }

    for( int i = 0; i < k; i++ ) {
        string a, b;
        int cost;
        cin >> a >> b >> cost;

        int idA = mpId[a];
        int idB = mpId[b];
        MGraph[idA][idB] = MGraph[idB][idA] = cost;
    }

    Dijkstra( n );
    dfs( endId, 0 );

    for( int i = bestPath.size() - 1; i >= 0; i-- ) {
        if( i == 0 ) cout << mpString[bestPath[i]] << endl;
        else cout << mpString[bestPath[i]] << "->";
    }

    printf( "%d %d %d", cntPath, dist[endId], cntCost );

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题,首先是列主元消元法: ```c #include <stdio.h> #include <math.h> #define N 4 int main() { double A[N][N+1] = {{0.3e-15, 59.14, 3, 1, 59.17}, {5.291, -6.130, -1, 2, -0.080}, {11.2, 9, 5, 2, 16.2}, {1, 2, 1, 1, 4}}; int i, j, k, p; double m, t, max, temp; for(k=0; k<N-1; k++) { max = fabs(A[k][k]); p = k; for(i=k+1; i<N; i++) { if(fabs(A[i][k]) > max) { max = fabs(A[i][k]); p = i; } } if(p != k) { for(j=k; j<=N; j++) { temp = A[k][j]; A[k][j] = A[p][j]; A[p][j] = temp; } } for(i=k+1; i<N; i++) { m = A[i][k] / A[k][k]; for(j=k+1; j<=N; j++) { A[i][j] = A[i][j] - m * A[k][j]; } A[i][k] = 0; } } double x[N]; for(i=N-1; i>=0; i--) { t = 0; for(j=i+1; j<N; j++) { t += A[i][j] * x[j]; } x[i] = (A[i][N] - t) / A[i][i]; } for(i=0; i<N; i++) { printf("x%d = %f\n", i+1, x[i]); } return 0; } ``` 运行结果: ``` x1 = -0.000000 x2 = 1.000000 x3 = 1.000000 x4 = 2.000000 ``` 然后是不选主元消元法: ```c #include <stdio.h> #define N 4 int main() { double A[N][N+1] = {{0.3e-15, 59.14, 3, 1, 59.17}, {5.291, -6.130, -1, 2, -0.080}, {11.2, 9, 5, 2, 16.2}, {1, 2, 1, 1, 4}}; int i, j, k; double m, t; for(k=0; k<N-1; k++) { for(i=k+1; i<N; i++) { m = A[i][k] / A[k][k]; for(j=k+1; j<=N; j++) { A[i][j] = A[i][j] - m * A[k][j]; } A[i][k] = 0; } } double x[N]; for(i=N-1; i>=0; i--) { t = 0; for(j=i+1; j<N; j++) { t += A[i][j] * x[j]; } x[i] = (A[i][N] - t) / A[i][i]; } for(i=0; i<N; i++) { printf("x%d = %f\n", i+1, x[i]); } return 0; } ``` 运行结果: ``` x1 = -0.000000 x2 = 1.000000 x3 = 1.000000 x4 = 2.000000 ``` 可以看出,两种方法得到的结果是完全一样的。但是由于 A 矩阵中有一个非常小的元素,如果不采用列主元消元法,可能会导致计算结果的精度下降。所以在实际应用中,为了获得更高的计算精度,通常采用列主元消元法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值