九度oj 1008
来源:互联网 发布:游戏编程c语言 编辑:程序博客网 时间:2024/06/10 09:23
- 题目描述:
- 给你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年浙江大学计算机及软件工程研究生机试真题
#include<iostream>#include<vector>#include<stdio.h>#define true 1#define false 0using namespace std;struct A{ int next; int c; int cost; }; vector<A> edge[1001]; bool mark[1001]; int cost[1001]; int dis[1001]; int main() { int n,m; int s,t; while(cin>>n>>m) { if(n==0&&m==0) { break;} for(int i=0;i<=n;i++) { edge[i].clear();} while(m--) { int a,b,c,cost; cin>>a>>b>>c>>cost; A tmp; tmp.c=c; tmp.next=b; tmp.cost=cost; edge[a].push_back(tmp); tmp.next=a; edge[b].push_back(tmp); } cin>>s>>t; for(int i=1;i<=n;i++) { dis[i]=-1; mark[i]=false; } 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; } cout<<dis[t]<<" "<<cost[t]<<endl; } }
0 0
- 九度OJ 1008
- 九度oj 1008
- 【九度OJ】:九度OJ 1050
- 【九度OJ】:九度OJ 1053
- 【九度OJ】:九度OJ 1056
- 【九度OJ】:九度OJ 1059
- 【九度OJ】:九度OJ 1206
- 九度oj 1008 最短路径
- 九度OJ:1000
- 【九度OJ】 1006
- 九度 OJ 1499
- 九度OJ-1002
- 九度OJ-1003
- 九度OJ 1004
- 九度OJ 1003
- 九度oj 采药
- 九度OJ 1001
- 九度OJ 1015
- 1070. Mooncake (25)
- InputStream、InputStreamReader和Reader的关系
- Android Parcelable和Serializable的区别(二)
- JAVA动态代理的作用
- Android错误之java.lang.RuntimeException: Binary XML file line #160: You must supply a layout_width attr
- 九度oj 1008
- 友元函数的几种形式
- HDOJ 1002
- 小点滴——word多级编号——摘自贴吧
- MongoDb的“not master and slaveok=false”错误及解决方法
- GraphX实现N度关系
- 写给软件学院的本科生(转)
- LeetCode-Excel Sheet Column Number
- 设置linux时间和时区 php时间等等