携程编程决赛-最短路径的代价

Problem Description
  
  
通常情况下找到N个节点、M 条边的无向图中某两点的最短路径很简单。假设每条边都有各自的长度以及删除这条边的代价,那么给出两个点X和Y,删除一些边可以使得X到Y的最短路径增加,求删除边最少的代价。
Input
  
  
第一行包含参数N和M,分别表示节点和边的个数 (2 ≤N≤ 1000,1 ≤M≤ 11000 ); 第二行包含参数X和Y,表示源点和终点( 1 ≤ X,Y ≤ N,X ≠ Y); 剩下M行每行包含4个参数Ui、Vi、Wi和Ci,分别表示节点Ui和Vi之间边的长度Wi以及删除的代价Ci ( 1 ≤ Ui , Vi ≤ N, 0 ≤Wi, Ci ≤1000 ); 如果某行N=M=0就表示输入结束。
Output
对于每个用例,按行输出增加X和Y直接最短路径所需要的最小代价。这个代价是所有删除的边的代价之和。
Sample Input
  
  
2 3 1 2 1 2 2 3 1 2 2 4 1 2 3 5 4 5 1 4 1 2 1 1 2 3 1 1 3 4 1 1 1 4 3 2 2 2 2 3 4 5 2 3 1 2 3 2 2 4 3 4 1 3 2 3 3 4 2 3 1 4 0 1 0 0
Sample Output
  
  
7 3 6
 
总结:因为求的比最短路径小的,所以我们只关心每两个顶点间路径长度最小的,然后累计其费用,因为路径可能存在多条(比如测试用例)所以可能要求多次最短路径
然后记录路径,寻找费用最小的,把这条(也可能是相同长度的多条)边标记一下,以后再次求解最短路径时候不再用此变(相当于已经删除了),再次求解最短路如果
所求的解和第一次相关着累加费用并标记路径中最小费用的边(可能多条相同长度的边)直到所求最路径比第一次小为止。
求解此题目需要注意的:用邻接矩阵比较有利于标记边(即删除)2.只关心链接两个顶点边集合中的最小长度的路径。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
const int INF = 0x3fffffff;
const int maxn = 1010;

int d[maxn][maxn]; // distance .
int cost[maxn][maxn]; // cost.
bool vis[maxn][maxn]; // mark;
int n, m;
int s, t; // start, end point.
int w, h; // mark the edges.

queue<int> que;
bool inque[maxn];
int p[maxn]; // parent.
int dis[maxn];
void spfa() {
    while(!que.empty()) que.pop();
    memset(inque, false, sizeof(inque));
    for(int i = 1; i <= n; i++) dis[i] = INF;
    dis[s] = 0;
    inque[s] = true;
    que.push(s);
    while(!que.empty()) {
        int u = que.front(); que.pop();
        for(int i = 1; i <= n; i++) {
            if(vis[u][i]) {
                if(dis[i] > dis[u]+d[u][i]) {
                    dis[i] = dis[u]+d[u][i];
                    p[i] = u;
                    if(!inque[i]) {
                        que.push(i);
                        inque[i] = true;
                    }
                }
            }
        }
        inque[u] = false;
    }
}

int work() {
    int ans = INF;
    int tmp = t; // end point.
    while(p[tmp] != s) {
        ans = min(ans, cost[tmp][p[tmp]]);
        tmp = p[tmp];
    }
    ans = min(ans, cost[s][tmp]);
    tmp = t;
    while(p[tmp]!=s) {
        if(cost[tmp][p[tmp]]==ans) {
            w = tmp;
            h = p[tmp];
        }
        tmp = p[tmp];
    }
    if(cost[s][tmp]==ans) {
        w = s;
        h = tmp;
    }
    vis[w][h] = false;
    vis[h][w] = false;
    return ans;
}

int main()
{
    int x, y, len, price;
    while(scanf("%d%d", &n, &m)==2) {
        if(n==0&&m==0) break;
        memset(vis, false, sizeof(vis));
        memset(d, -1, sizeof(d));
        scanf("%d%d", &s, &t);
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d%d", &x, &y, &len, &price);
            if(d[x][y]==-1)  { // first time.
                d[x][y] = len;
                d[y][x] = len;
                cost[x][y] = price;
                cost[y][x] = price;
                vis[x][y] = true;
                vis[y][x] = true;
                continue;
            }
            else if(d[x][y]==len) {
                cost[x][y] += price;  // 累计。
                cost[y][x] += price;
            }
            else if(d[x][y]>len) {
                d[x][y] = len;
                d[y][x] = len;
                cost[x][y] = price; // change.
                cost[y][x] = price;
            }
        }
        spfa();
        int shortest = dis[t];
        int res = work();
        while(dis[t]==shortest) {
            spfa();
            int tmp = work();
            if(dis[t]==shortest) {
                res += tmp;
            } else {
                break;
            }
        }
        printf("%d\n", res);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值