九度oj 题目1008:最短路径问题
来源:互联网 发布:免费工程造价软件 编辑:程序博客网 时间:2024/06/05 05:26
- 题目描述:
- 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
- 输入:
- 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点t。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
- 输出:
- 输出 一行有两个数, 最短距离及其花费。
- 样例输入:
3 21 2 5 62 3 4 51 30 0
- 样例输出:
9 11
- 来源:
2010年浙江大学计算机及软件工程研究生机试真题
1.地杰斯特拉算法。
2.在判断路径代价相同时,才需考虑费用花销。
#include <cstdio> #include <vector> using namespace std; struct E{ int next; int c; int cost;}; vector<E> edge[1001];int Dis[1001];int cost[1001];bool mark[1001]; int main(){ int n,m; int S,T; //freopen("input.txt","r",stdin); while((scanf("%d%d",&n,&m)!=EOF)&&!(n==0&&m==0)){ for(int i =1;i<=n;i++) edge[i].clear(); while(m--){ int a,b,c,cost; scanf("%d%d%d%d",&a,&b,&c,&cost); E tmp; tmp.c = c; tmp.cost = cost; tmp.next = b; edge[a].push_back(tmp); tmp.next = a; edge[b].push_back(tmp); } scanf("%d%d",&S,&T); for(int i =1;i<=n;i++){ Dis[i] = -1; mark[i] = false; } cost[S] = 0; Dis[S] = 0; mark[S] = true; int newP = S; for(int i=1;i<n;i++){ for(int j =0;j<edge[newP].size();j++){ int t = edge[newP][j].next; int c = edge[newP][j].c; int co = edge[newP][j].cost; if(mark[t] == true) continue; if(Dis[t]==-1 || Dis[t]>Dis[newP]+c||Dis[t]==Dis[newP]+c && cost[t]>cost[newP]+co){ Dis[t] = Dis[newP] + c; cost[t] = cost[newP] + co; } } int min = 123123123; for(int j =1;j<=n;j++){ if(mark[j]==true) continue; if(Dis[j]==-1) continue; if(Dis[j]<min){ min = Dis[j]; newP = j; } } mark[newP] = true; } printf("%d %d\n",Dis[T],cost[T]); } return 0; }/************************************************************** Problem: 1008 User: hankcheung Language: C++ Result: Accepted Time:10 ms Memory:1056 kb****************************************************************/
0 0
- 九度oj 题目1008:最短路径问题
- 九度oj 题目1008:最短路径问题
- 九度OJ 题目1008:最短路径问题
- 九度OJ:题目1008 最短路径问题
- 九度 oj 题目1008:最短路径问题
- 九度OJ-题目1008 最短路径问题
- 九度oj 题目1008:最短路径问题
- 九度oj 题目1008:最短路径问题 【ZJU2010考研机试题4】【dij+dfs】
- 九度 OJ 题目1008:最短路径问题 (Dijstra 算法)
- 九度OJ 1008最短路径问题
- 九度OJ--1008--最短路径问题
- 九度oj-1008-最短路径问题
- 九度OJ 1008 最短路径问题
- 九度 OJ 1008 最短路径问题
- 九度OJ-浙大1008最短路径问题
- 九度OJ-1008:最短路径问题
- 九度OJ——1008最短路径问题
- 九度:题目1008:最短路径问题
- 【JZOJ5056】【GDSOI2017模拟4.13】黑白广场
- 处理CFormView类 -- 保护类型的构造函数和Create函数(讨厌的东西!!!)
- vi编辑器常用的命令及快捷键
- 算法面试题:使用两个堆栈实现一个队列
- android学习笔记(2)
- 九度oj 题目1008:最短路径问题
- 抽象类和匿名方法的使用
- 奇葩设置能加快Android studio的运行效率
- Spring Boot配置数据库链接池
- Frame、JPanel、JApplet的布局管理器是什么
- 方法重写与super限定
- java对象和类
- 1094. The Largest Generation (25)
- 数组类型 初始化