携程编程决赛-最短路径的代价
来源:互联网 发布:联想固态硬盘优化软件 编辑:程序博客网 时间:2024/06/08 13:25
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 31 21 2 2 31 2 2 41 2 3 54 51 41 2 1 12 3 1 13 4 1 11 4 3 22 2 2 34 52 31 2 3 22 4 3 41 3 2 33 4 2 31 4 0 10 0
Sample Output
736
总结:因为求的比最短路径小的,所以我们只关心每两个顶点间路径长度最小的,然后累计其费用,因为路径可能存在多条(比如测试用例)所以可能要求多次最短路径
然后记录路径,寻找费用最小的,把这条(也可能是相同长度的多条)边标记一下,以后再次求解最短路径时候不再用此变(相当于已经删除了),再次求解最短路如果
所求的解和第一次相关着累加费用并标记路径中最小费用的边(可能多条相同长度的边)直到所求最路径比第一次小为止。
求解此题目需要注意的:用邻接矩阵比较有利于标记边(即删除)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
- 携程编程决赛-最短路径的代价
- 携程编程大赛决赛-1004-最短路径的代价
- USTC 1280 / 携程决赛1004 最短路径的代价
- USTC 1280 / 携程决赛1004 最短路径的代价
- HDU携程决赛最短路径的代价/USTC 1280 Finding Shortest Path 求最短路边+最小割
- hdu-携程复赛-最短路径的代价-抠图+最小割
- Opnet 一个简单的网络实现Dijkstra最短路径算法,路径代价为跳数加排队延时
- Windows Shell编程-第六章.快捷方式的最短路径
- 编程算法 - 迷宫的最短路径 代码(C++)
- 最短路径的实现
- 迷宫的最短路径
- 最短路径的实现
- 迷宫的最短路径
- 图的最短路径
- 再谈图的最短路径
- 迷宫的最短路径
- 图的最短路径
- 最短路径的实现
- 【LB】C语言实现贪吃蛇
- java实现逆序数对
- Sun-JNI文档系列之六——第五章:局部引用与全局引用
- Android屏幕分辨率获取方法--源码剖析
- HashMap、TreeMap、Hashable和LinkedHashMap
- 携程编程决赛-最短路径的代价
- Hibernate框架学习(一 )
- Android 数据库优化
- Sun-JNI文档系列之七——第六章:异常
- Eclipse Tools
- 岁月如梭,从2007年8月接触SAP至今已有6.5年
- Linux 创建符号链接
- 第一个C语言程序
- 实验室项目svc& android& mplayer