SDUT 3363 数据结构实验之图论七:驴友计划
数据结构实验之图论七:驴友计划
Problem Description
Input
连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 <= N <= 500)是城市数目,城市编号从0~N-1,M是城市间高速公路的条数,s是出发地的城市编号,d是目的地的城市编号;随后M行,每行给出一条高速公路的信息,表示城市1、城市2、高速公路长度、收费额,中间以空格间隔,数字均为整数且不超过500,输入数据均保证有解。
Output
在同一行中输出路径长度和收费总额,数据间用空格间隔。
Example Input
1
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Example Output
3 40
DQE:
#include <iostream>
#include <cstdio>
#include <climits> using namespace std; #define MVN 510 typedef struct AdjMatrix
{
int w; //距离
int m; //费用
char *info;
}AM; typedef struct MGraph
{
int vex[MVN];
AM arc[MVN][MVN];
int vexn,arcn;
int s,e; //起点,终点(编号-下标)
}MG; void creat(MG &G)
{
int i,j,w,m,k;
for(i=;i<G.vexn;i++)
for(j=;j<G.vexn;j++)
{
G.arc[i][j].w=INT_MAX;
G.arc[i][j].m=;
}
for(k=;k<G.arcn;k++)
{
scanf("%d %d %d %d",&i,&j,&w,&m);
G.arc[i][j].w=G.arc[j][i].w=w;
G.arc[i][j].m=G.arc[j][i].m=m;
}
} struct{int w,m;}D[MVN]; //辅助数组{当前最短距离和,当前费用和}
void short_p(MG &G)
{
bool f[MVN]={false}; //是否锁定为最短路径
int i,j,k,min;
//初始化辅助数组并锁定起点
for(k=;k<G.vexn;k++)
{
D[k].w=G.arc[G.s][k].w;
D[k].m=G.arc[G.s][k].m;
}
f[G.s]=true;
//选择并锁定下一最短路径顶点
for(k=;k<G.vexn;k++)
{
min=INT_MAX;
for(j=;j<G.vexn;j++)
if(!f[j])
if(D[j].w<min)
{
i=j;
min=D[j].w;
}
f[i]=true;
//更新该顶点的邻接点的最短路
for(j=;j<G.vexn;j++)
if(!f[j]&&G.arc[i][j].w<INT_MAX)
if(min+G.arc[i][j].w<D[j].w)
{
D[j].w=min+G.arc[i][j].w;
D[j].m=D[i].m+G.arc[i][j].m;
}
else if(min+G.arc[i][j].w==D[j].w && D[i].m+G.arc[i][j].m<D[j].m) //距离相同时选择费用更少
D[j].m=D[i].m+G.arc[i][j].m;
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
MG G;
scanf("%d %d %d %d",&G.vexn,&G.arcn,&G.s,&G.e);
creat(G);
short_p(G);
printf("%d %d\n",D[G.e].w,D[G.e].m);
}
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 176KB
Submit time: 2016-11-17 18:23:42
****************************************************/
解法二(不推荐):深度优先搜索,注意搜索过程中的回退需要清除相关标记。
#include <iostream>
#include <cstdio>
#include <climits> using namespace std; #define MVN 550 typedef struct AdjMatrix
{
int w;
int m;
char *info;
}AM; typedef struct MGraph
{
int vex[MVN];
AM arc[MVN][MVN];
int vexnum,arcnum;
int s,d;
int w,m;
int minw,minm;
}MG; void creat(MG &G)
{
int i,j,w,m,k;
for(k=;k<G.vexnum;k++)
G.vex[k]=k;
for(k=;k<G.arcnum;k++)
{
scanf("%d %d %d %d",&i,&j,&w,&m);
G.arc[i][j].w=w;
G.arc[i][j].m=m;
G.arc[j][i].w=w;
G.arc[j][i].m=m;
}
} void DFS(MG &G,bool *visited,int i)
{
visited[i]=true;
if(i==G.d)
{
if(G.w<G.minw || G.w==G.minw && G.m<G.minm)
{
G.minw=G.w;
G.minm=G.m;
}
}
else
{
int k;
for(k=;k<G.vexnum;k++)
{
if(G.arc[i][k].w<INT_MAX&&visited[k]==false)
{
G.w+=G.arc[i][k].w;
G.m+=G.arc[i][k].m;
DFS(G,visited,k); visited[k]=false; //※探索不同路径后的回退操作※
G.w-=G.arc[i][k].w;
G.m-=G.arc[i][k].m;
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
MG G={};
scanf("%d %d %d %d",&G.vexnum,&G.arcnum,&G.s,&G.d);
int k,o;
for(k=;k<G.vexnum;k++)
{
for(o=;o<G.vexnum;o++)
G.arc[k][o].w=INT_MAX;
}//初始化邻接矩阵
creat(G);
bool visited[MVN]={false};
G.minm=INT_MAX; //初始化一个最大值
G.minw=INT_MAX;
DFS(G,visited,G.s);
printf("%d %d\n",G.minw,G.minm);
}
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 2832KB
Submit time: 2016-11-09 22:46:01
****************************************************/
SDUT 3363 数据结构实验之图论七:驴友计划的更多相关文章
- 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )
数据结构实验之图论七:驴友计划 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT 3364 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...
- SDUT 3346 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)
数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
随机推荐
- 设置SSH自动登陆(免密码,用户名)
设置SSH自动登陆(免密码,用户名) 1.创建公钥.公钥 ssh-keygen -t rsa 无视它出来的任何提示,欢快的一路回车到底吧. 2.把公钥 id_rsa.pub 复制到远程机器的 ...
- Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(二)
用mogoose搭建restful测试接口 接着上一篇(Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一))记录,今天单独搭建一个restful测试接口,和项目前 ...
- Node.js + Express
相关链接: 1.https://www.jianshu.com/p/db4df1938eca 2.前端发起GET请求:http://localhost:3000/api/login?name=admi ...
- 3、Selenium调用IEDriverServer打开IE浏览器
学习Selenium时若想调用IE浏览器,均需要以下步骤 (1).http://selenium-release.storage.googleapis.com/index.html 下载IEDrive ...
- Azure disk iops的测试
在Public Cloud中,VM.Storage和Network是IaaS的三大基础.本文将介绍在Azure的VM上测试磁盘IOPS的工具和方法. 一.添加磁盘.初始化磁盘 1.添加磁盘 把相应的信 ...
- Linux驱动多线程 - 互斥量
1.内核多线程相关内容 1.1 头文件#include <linux/kthread.h> 1.2 定义/初始化变量 struct mutex SPI_work; /*定义互斥体*/ mu ...
- AngularJS中的http服务的简单用法
我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏览器原生的XMLHttpRequest对象. 1.链式调用 $http服务是只能接受一个参数的函数,这个参数是一个对 ...
- [我的CVE][CVE-2017-15709]Apache ActiveMQ Information Leak
问题原因: Apache ActiveMQ默认消息队列61616端口对外,61616端口使用了OpenWire协议,这个端口会暴露服务器相关信息,这些相关信息实际上是debug信息. 会返回应用名称, ...
- jenkins基础知识
修改默认端口号启动: java -jar jenkins.war --ajp13Port=-1 --httpPort=8089 一些基本的命令: http://[jenkins-server]/[co ...
- Python 标准库 -> Pprint 模块 -> 用于打印 Python 数据结构
使用 pprint 模块 pprint 模块( pretty printer ) 用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读). ...