题目1008:最短路径问题
来源:互联网 发布:msa是什么软件啊 编辑:程序博客网 时间:2024/06/06 08:55
#include "iostream"#include "stdio.h"#include <vector>#include <cstring>#include <algorithm>#include <string>#include <string.h>#include <stack>#include <ctype.h>#include <functional>#include <math.h>using namespace std;const int INF=0x7ffffff;struct E{ int nt,d,p; //邻接点 路径 花费};vector<E> edge[1001];int ds[1001],cs[1001]; //记录最短路径 最少花费bool mark[1001]; //标记是否属于集合Kint n,m,s,e;E tmp;int main(){ //freopen("input.txt","r",stdin); while(cin>>n>>m && n && m){ for(int i=1;i<=n;i++) edge[i].clear(); while(m--){ int a,b,d,p; cin>>a>>b>>d>>p; tmp.d=d;tmp.p=p; tmp.nt=b; edge[a].push_back(tmp); tmp.nt=a; edge[b].push_back(tmp); } cin>>s>>e; for(int i=1;i<=n;i++) mark[i]=false,ds[i]=INF,cs[i]=0; ds[s]=0,mark[s]=true; int np=s; for(int i=1;i<n;i++){ //循环n-1次 for(int j=0;j<edge[np].size();j++){ int t=edge[np][j].nt; int d=edge[np][j].d; int p=edge[np][j].p; if(mark[t]) continue; if(ds[t]==INF || ds[t]>ds[np]+d || ds[t]==ds[np]+d && cs[t]>cs[np]+p ){ ds[t]=ds[np]+d; cs[t]=cs[np]+p; } } int mi=INF; for(int j=1;j<=n;j++){ if(mark[j]) continue; if(ds[j]<mi) mi=ds[j],np=j; } mark[np]=true; } cout<<ds[n]<<" "<<cs[n]<<endl; } return 0;}/************************************************************** Problem: 1008 User: cust123 Language: C++ Result: Accepted Time:10 ms Memory:1552 kb****************************************************************/
#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>#include <string>#include <string.h>#include <vector>#include <queue>#include <stack>#include <sstream>using namespace std;//1008const int INF=1001000000;const int maxn=1e3+10;int mp_d[maxn][maxn],mp_c[maxn][maxn];int vis[maxn],dis[maxn],cost[maxn];int n,m;void dijkstra(int s){ for(int i=1;i<=n;i++){ dis[i]=mp_d[s][i]; cost[i]=mp_c[s][i]; vis[i]=0; } vis[s]=1;dis[s]=0;cost[s]=0; for(int i=1;i<=n;i++){//循环遍历n个顶点 int min_dis=INF,idx; for(int j=1;j<=n;j++){//寻找未访问的直接相邻的最短边 if(vis[j]==0&&dis[j]<min_dis){ min_dis=dis[j]; idx=j; } } vis[idx]=1; for(int k=1;k<=n;k++){ if(dis[k]>dis[idx]+mp_d[idx][k]){ dis[k]=dis[idx]+mp_d[idx][k]; cost[k]=cost[idx]+mp_c[idx][k]; } else if(dis[k]==dis[idx]+mp_d[idx][k]){ cost[k]=min(cost[idx]+mp_c[idx][k],cost[k]); } } }}int main(){ // freopen("input.txt","r",stdin); while(scanf("%d %d",&n,&m)!=EOF){ if(n==0&&m==0) break; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) mp_d[i][j]=mp_c[i][j]=INF; while(m--){ int a,b,d,p; scanf("%d%d%d%d",&a,&b,&d,&p); mp_d[a][b]=mp_d[b][a]=d; mp_c[a][b]=mp_c[b][a]=p; } int s,e; scanf("%d %d",&s,&e); dijkstra(s); cout<<dis[e]<<" "<<cost[e]<<endl; } return 0;}/************************************************************** Problem: 1008 User: cust123 Language: C++ Result: Accepted Time:20 ms Memory:9500 kb****************************************************************/
0 0
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- Jobdu 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题 java实现
- 九度oj 题目1008:最短路径问题
- 九度:题目1008:最短路径问题
- 九度题目1008:最短路径问题
- 从注册表的键值判断OS是32位还是64位
- Web前端复习——Javascript复习(数值转换+运算符)
- 菜单文章的补充
- 有关删除数字的dp问题
- VS2013怎么显示行号和程序反汇编代码
- 题目1008:最短路径问题
- SQL事务隔离级别详解
- 数据结构(15)——--邻接矩阵存储练习
- Linux第十一单元练习题
- c++ 标准输入输出流基础
- 调试原理
- Android对话框AlertDialog.Builder使用方法
- 针对Android的Crash监控, 崩溃分析---推荐Bugly
- 24点游戏