HDU 3790最短路径问题
来源:互联网 发布:网络安全大数据分析 编辑:程序博客网 时间:2024/06/06 09:08
用的spfa的思想写的,我感觉用一个标记数组flag[]来标志是否在队列中,好像不是很有用啊,只要做了松弛操作,我感觉都应该入队才是吧= =。这道题目就是更新路径的时候顺便更新一下花费。
#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <vector>using namespace std;const int INF = 0x3f3f3f;const int MAX = 1e3 + 5;int d[MAX], p[MAX];//, flag[MAX];struct EDGE{ int to, cost, price; EDGE(int a = 0, int b = 0, int c = 0) : to(a), cost(b), price(c) {}};vector<EDGE> que[MAX];queue<int> coor;int main(){ int n, m; while (scanf("%d%d", &n, &m)) { if (n == 0 && m == 0) { break; } for (int i = 1; i <= n; ++i) que[i].clear(); int t_from, t_to, t_cost, t_price; //memset(flag, 0, sizeof(flag)); memset(d, INF, sizeof(d)); memset(p, INF, sizeof(p)); for (int i = 0; i < m; ++i) { scanf("%d%d%d%d", &t_from, &t_to, &t_cost, &t_price); que[t_from].push_back(EDGE(t_to, t_cost, t_price)); que[t_to].push_back(EDGE(t_from, t_cost, t_price)); } int st, ed; scanf("%d%d", &st, &ed); // flag[st] = 1; d[st] = 0; p[st] = 0; coor.push(st); int res = INF; while (!coor.empty()) { int t_coor = coor.front(); coor.pop(); //flag[t_coor] = 0; int lenth = que[t_coor].size(); for (int i = 0; i < lenth; ++i) { EDGE t_edge = que[t_coor][i]; if (d[t_edge.to] > d[t_coor] + t_edge.cost) { d[t_edge.to] = d[t_coor] + t_edge.cost; // printf("from(%d) to(%d)\n", t_coor, t_edge.to); // printf("shortest path = %d price = %d\n", d[ed], p[ed]); // if (p[t_edge.to] >= p[t_coor] + t_edge.price) p[t_edge.to] = p[t_coor] + t_edge.price; //printf("now price = %d\n", p[ed]); // res = min(res, p[ed]); coor.push(t_edge.to); } else if (d[t_edge.to] == d[t_coor] + t_edge.cost && p[t_edge.to] > p[t_coor] + t_edge.price) p[t_edge.to] = p[t_coor] + t_edge.price; } } printf("%d %d\n", d[ed], p[ed]); } return 0;}
0 0
- 【最短路径-Dis】hdu 3790 最短路径问题
- hdu 3790 最短路径问题
- hdu 3790 最短路径问题
- HDU-3790-最短路径问题
- HDU 3790 最短路径问题
- hdu 3790 最短路径问题
- Hdu 3790 最短路径问题
- HDU 3790 最短路径问题
- HDU 3790 最短路径问题
- hdu 3790 最短路径问题
- hdu 3790 最短路径问题
- 最短路径问题 hdu 3790
- hdu 3790 最短路径问题
- hdu 3790 最短路径问题
- hdu--最短路径问题--3790
- HDU 3790 最短路径问题
- hdu 3790 最短路径问题
- HDU 3790 最短路径问题 (SPFA)
- 数据库问题
- 程序员经验
- [面试] 算法(六)—— 链表
- window.open窗口显示在屏幕中央
- mysql command line client 使用命令
- HDU 3790最短路径问题
- BZOJ 4423 AMPPZ 2013 Bytehattan 平面图 并查集
- 将导航栏固定在窗口的顶部
- css div靠最下
- Android新手入门2016(15)--Gallery画廊
- Android基础-------button的三种定义方式
- Linux文件系统
- POJ 2886 Who Gets the Most Candies?(线段树、模拟)
- Quartz任务调度(2)CronTrigger定制个性化调度方案