代码随想录 打卡第六十/六十一天

卡码网 94 城市间货物运输 I Bellman-ford算法(队列优化)

cpp 复制代码
#include<iostream>
#include<vector>
#include<climits>
#include<queue>
#include <list>
using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    vector<list<pair<int,int>>> graph(n+1);
    int x,y,w;
    for(int i = 0;i < m;i++){
        cin >> x >> y >> w;
        graph[x].push_back({y , w});
    }

    vector<int> mindistance(n+1,INT_MAX);
    vector<bool> inque(n+1,false);
    queue<int> que;
    que.push(1);
    mindistance[1] = 0; 

    while(!que.empty()){
        int cur = que.front();
        que.pop();
        inque[cur] = false; 
        for(pair<int,int>& edge : graph[cur]){
                int from = cur;
                int to = edge.first;
                int val = edge.second;
                if(mindistance[from] + val < mindistance[to]){
                    mindistance[to] = mindistance[from] + val;
                    if(inque[to] == false){
                        que.push(to);
                        inque[to] = true;
                    }
                }
        }
    }

    if(mindistance[n] == INT_MAX) cout << "unconnected" << endl;
    else cout << mindistance[n] << endl;

    return 0;
}

卡码网 95 城市间货物运输 II Bellman_ford算法判断负权回路

cpp 复制代码
#include<iostream>
#include<vector>
#include<climits>

using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>> edges(m,vector<int>(3,0));
    int x,y,w;
    for(int i = 0;i < m;i++){
        cin >> x >> y >> w;
        edges[i][0] = x;
        edges[i][1] = y;
        edges[i][2] = w;
    }

    vector<int> mindistance(n+1,INT_MAX);
    mindistance[1] = 0;
    for(int i = 1;i < n;i++){
        for(vector<int> edge : edges){
            int from = edge[0];
            int to = edge[1];
            int val = edge[2];
            if(mindistance[from] != INT_MAX && mindistance[from] + val < mindistance[to]){
                mindistance[to] = mindistance[from] + val;
            }
        }
    }
    for(vector<int> edge : edges){
        int from = edge[0];
        int to = edge[1];
        int val = edge[2];
        if(mindistance[from] != INT_MAX && mindistance[from] + val < mindistance[to]){
            cout << "circle";
            return 0;
        }
    }

    if(mindistance[n] == INT_MAX) cout << "unconnected";
    else cout << mindistance[n];
    
    return 0;
}

卡码网 96 城市间货物运输 III Bellman_ford算法单源有限最短路

cpp 复制代码
#include<iostream>
#include<vector>
#include<climits>
#include<stack>
using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>> graph;
    int x,y,w;
    for(int i = 0;i < m;i++){
        cin >> x >> y >> w;
        graph.push_back({x,y,w});
    }
    int start,end,k;
    cin >> start >> end >> k;
    vector<int> mindistance(n+1,INT_MAX);

    mindistance[start] = 0;
    vector<int> temp = mindistance;

    for(int i = 0;i < k+1;i++){
        temp = mindistance;
        for(vector<int>& side : graph){
            int from = side[0];
            int to = side[1];
            int val = side[2];
            if(temp[from] != INT_MAX && temp[from] + val < mindistance[to]){
                mindistance[to] = temp[from] + val;
            }
        }
    }
    if(mindistance[end] == INT_MAX) cout <<  "unreachable" << endl;
    else cout << mindistance[end] << endl;
 
    return 0;
}

卡码网 97 小明逛公园 Floyd算法

cpp 复制代码
#include<iostream>
#include<vector>
#include <climits>
using namespace std;
 
int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(n+1,INT_MAX/2)));
    int x,y,w;
    for(int i = 0;i < m;i++){
        cin >> x >> y >> w;
        dp[x][y][0] = w;
        dp[y][x][0] = w;
    }
    for(int i = 1;i <=n ;i++){
        dp[i][i][0] = 0;
    }
    
    for(int k = 1;k <=n ;k++){
        for(int i = 1;i <=n ;i++){
            for(int j = 1;j <=n ;j++){
                dp[i][j][k] = min(dp[i][j][k-1],dp[i][k][k-1]+dp[k][j][k-1]);
            }
        }
        
    }

    int start,end;
    vector<vector<int>> plan;
    cin >> m;
    for(int i = 0;i < m;i++){
        cin >> start >> end;
        if(dp[start][end][n] == INT_MAX/2) cout << -1 << endl;
        else cout << dp[start][end][n] << endl;
    }
    return 0;
}

卡码网 127 骑士的攻击 A*算法

cpp 复制代码
#include <iostream>
#include <queue>
#include<string.h>
using namespace std;

int x_1,x_2,y_1,y_2;
int chessboard[1001][1001];
int driver[8][2] = {2,1, 2,-1, -2,1, -2,-1, 1,2, 1,-2, -1,2, -1,-2};

struct knight{
    int x,y,g,h,f;
    bool operator < (const knight& k) const {
        return k.f < f;
    }
};
priority_queue<knight> que;

int he(const knight& k){
    return (k.x - x_2)*(k.x - x_2) + (k.y - y_2)*(k.y - y_2);
}

void A(const knight& k){
    knight cur,next;
    que.push(k);
    while(!que.empty()){
        cur = que.top();
        que.pop();
        if(cur.x == x_2 && cur.y == y_2) break;
        for(int i = 0;i < 8;i++){
            next.x = cur.x + driver[i][0];
            next.y = cur.y + driver[i][1]; 
            if(next.x >= 1 && next.x <= 1000 && next.y >= 1 && next.y <= 1000){
                if(chessboard[next.x][next.y] == 0){ 
                    chessboard[next.x][next.y] = chessboard[cur.x][cur.y] + 1;
                    next.g = cur.g + 5;
                    next.h = he(next);
                    next.f = next.g + next.h;
                    que.push(next);
                }
            }
        }
    }
}
int main(){
    int m;
    cin >> m;
    vector<knight> result;
    while(m--){
        cin >> x_1 >> y_1 >> x_2 >> y_2;
        memset(chessboard,0,sizeof(chessboard));
        knight k;
        k.x = x_1;
        k.y = y_1;
        k.g = 0;
        k.h = he(k);
        k.f = k.g + k.h;
        A(k);
        while(!que.empty()) que.pop();
        cout << chessboard[x_2][y_2] << endl;
    }
    return 0;    
}