九度oj题目1008:最短路径问题
题目描述:
- 输入:
-
输入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 2
1 2 5 6
2 3 4 5
1 3
0 0
- 样例输出:
-
9 11
通过这道题复习了一下迪杰特斯拉算法,算法的精髓在于每回找一个距离集合最近的点加到集合中,直到不能加入更多的点为止
代码如下:#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#define MAX 999999999
#define N 1005
#define M 100005 struct Way {
int distance;
int cost;
}; Way map[N][N];//邻接矩阵
Way dis[N];//起点到各个点的距离表
int flag[N];//判别某点是否在已包括点的集合中 void dijkstra(int start, int end,int n) {
memset(flag,,sizeof(int)*N);
//初始化距离
for(int i = ; i <= n; i++) {
dis[i] = map[start][i];
}
dis[start].distance = ;//该点到自己的距离为0
dis[start].cost = ;//到自己的花费为0
flag[start] = ;//把自己包括到可以到达的点中 for(int i = ; i < n; i++) {
//总共需要加入n-1个点,所以循环n-1次
int index = ;
int min = MAX;
for(int j = ; j <= n; j++) {
if(flag[j] == && dis[j].distance < min) {
index = j;
min = dis[j].distance;
}
}
//find the min index
flag[index] = ; for(int j = ; j <= n; j++) {
if(flag[j] == && (dis[index].distance + map[index][j].distance < dis[j].distance)) {
dis[j].distance = dis[index].distance + map[index][j].distance;
dis[j].cost = dis[index].cost + map[index][j].cost;
}
else if(flag[j] == && (dis[index].distance + map[index][j].distance == dis[j].distance)){
if(dis[j].cost > dis[index].cost + map[index][j].cost) {
dis[j].cost = dis[index].cost + map[index][j].cost;
}
}
}
} } int main(int argc, char const *argv[])
{
int n,m;
int source, des;
scanf("%d %d",&n,&m);
while(n != && m != ) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
map[i][j].distance = MAX;
map[i][j].cost = MAX;
}
}
int pointA,pointB,disc,cost;
for(int i = ; i < m; i++) {
scanf("%d %d %d %d",&pointA,&pointB,&disc,&cost);
map[pointA][pointB].distance = disc;
map[pointB][pointA].distance = disc;
map[pointA][pointB].cost = cost;
map[pointB][pointA].cost = cost;
}
scanf("%d %d",&source,&des); dijkstra(source,des,n);
printf("%d %d\n",dis[des].distance,dis[des].cost);
scanf("%d %d",&n,&m);
}
return ;
}最近尝试了一下用优先队列来解决问题,感觉写得很别扭,不过还是写出来了,速度提高不少,代码如下
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue> #define MAX_V 1002
#define inf 999999999
using namespace std; struct Edge
{
public:
int to;
int dis;
int cost;
Edge(int to, int dis,int cost) {
this->to = to;
this->dis = dis;
this->cost = cost;
}
}; struct cmp{
bool operator()(const Edge &a,const Edge &b){
if(a.dis == b.dis) {
return a.cost - b.cost;
}
else {
return a.dis - b.dis;
}
}
};
typedef pair<int , int> P;//first dis, sencond cost
P dist[MAX_V];
vector<Edge> G[MAX_V];
priority_queue<Edge, vector<Edge>, cmp> que; int main(int argc, char const *argv[])
{
int n,m;
int source, des;
freopen("input.txt","r",stdin);
while(scanf("%d %d",&n,&m) != EOF && (n != && m != )) {
for(int i = ; i <= n; i++) {
G[i].clear();
}
for(int i = ; i < m; i++) {
int a, b, disc, cost;
scanf("%d %d %d %d",&a,&b,&disc,&cost);
G[a].push_back(Edge(b,disc,cost));
G[b].push_back(Edge(a,disc,cost));
}
scanf("%d %d",&source,&des);
fill(dist,dist+n+,P(inf,inf));
dist[source] = P(,);
que.push(Edge(source,,)); while(!que.empty()) {
Edge t = que.top(); que.pop();
int v = t.to, d = t.dis, c = t.cost;
if(dist[v].first < d) {
continue;
}
for(int i = ; i < G[v].size(); i++) {
Edge &e = G[v][i];
int d2 = d + e.dis;
int c2 = c + e.cost; if(dist[e.to].first > d2 || (dist[e.to].first == d2 && dist[e.to].second > c2)) {
dist[e.to] = P(d2,c2);
que.push(Edge(e.to,d2,c2));
}
}
}
printf("%d %d\n",dist[des].first,dist[des].second); }
return ;
}
九度oj题目1008:最短路径问题的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- Opencv内存jpg图像解码和编码[转]
CV_IMPL CvMat* cvEncodeImage( const char* ext,const CvArr* arr, const int* _params ) CV_IMPL IplImag ...
- IOS拉伸之底盖设置
1.选定拉伸 UIImageView *fieldImage=[[UIImageViewalloc]initWithFrame:CGRectMake(37,48+35,240, 32)]; field ...
- ajax上传文件以及使用中常见问题处理
<script src="/scripts/ajaxfileupload.js"></script> <script src="/scrip ...
- HDU 4044 GeoDefense (树形DP,混合经典)
题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...
- ActiveAndroid问题no such table解决总结
android.database.sqlite.SQLiteException: no such table at android.database.sqlite.SQLiteConnection ...
- python 基础网络编程1
python 基础网络编程1 Source code: Lib/socketserver.py lib的主目录下有一个sockserver.py文件, 里面是python基本的网络编程模型 共有一个b ...
- crontab 应用
可以用crontab -e 添加要执行的命令. 命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户. 添加的命令必须以如下格式: * * * * * /co ...
- (四)VMware Harbor 配置文件
VMware Harbor 配置文件 :harbor.yml # Configuration file of Harbor # The IP address or hostname to access ...
- vue-awesome-swiper 插件
Swiper 版本区分了组件和普通版本 (1)npm install vue-awesome-swiper –save (2)在 main,js 里引入(全局): import VueAwesomeS ...
- gson对象的相互转换
参见 http://www.javacreed.com/gson-deserialiser-example/