题目1008:最短路径问题
来源:互联网 发布:淘宝应用开发者平台 编辑:程序博客网 时间:2024/06/10 06:12
// 迪杰斯特拉算法
#include<stdio.h>
#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;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0 && m==0) break;
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;
}
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;
}
0 0
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- Jobdu 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题 java实现
- 九度oj 题目1008:最短路径问题
- 九度:题目1008:最短路径问题
- 九度题目1008:最短路径问题
- OpenMP与C++:事半功倍地获得多线程的好处(下)
- CareerCup Find top k values (asec) which can either be the number from the array A
- Android JNI调用(二)
- LINK : fatal error LNK1104: cannot open file "Debug/0123.exe"
- linux下显示dd命令的进度
- 题目1008:最短路径问题
- 关于SpringMVC事务失效的分析
- 使用mipsel-linux-gcc时出现No such file or directory (解决)
- Automake基本用法
- Android 增量更新 bsdiff bspatch
- 编写简单的在线考试系统
- Android JNI调用(三)
- SQL Server 脱机 联机
- NYOJ - 104 最大和【DP】转自:飘过的小牛的博客