1111. Online Map (30)
2016-04-07 20:51
489 查看
题目链接
#include <iostream> #include<stdio.h> #include<limits.h> #include<vector> #define MAX 501 using namespace std; typedef struct Map { int one_way; int length; int time; }MGraph; MGraph graph[MAX][MAX]; int curTime=0,curLength=0; int minTime=INT_MAX,minLength=INT_MAX,minLT=INT_MAX,minTL=INT_MAX; bool isReached[MAX]; vector<int> minLRoad,minTRoad,curRoad; ///只有一条合格的路 void DFS(int cur,int end) { if(curLength>minLength&&curTime>minTime) return; if(cur==end) { bool lengthBool=false,timeBool=false; if(curLength<minLength) { lengthBool=true; } else if(curLength==minLength) { if(curTime<minLT) lengthBool=true; } if(lengthBool) { minLength=curLength; minLRoad=curRoad; minLT=curTime; } if(curTime<minTime) { timeBool=true; } else if(curTime==minTime) { if(curLength<minTL) timeBool=true; } if(timeBool) { minTime=curTime; minTRoad=curRoad; minTL=curLength; } return; } for(int i=0;i<MAX;i++) { if(!isReached[i]&&(graph[cur][i]).time>0)///v1 to v2 { isReached[i]=true; curLength+=graph[cur][i].length; curTime+=graph[cur][i].time; curRoad.push_back(i); DFS(i,end); curRoad.pop_back(); curLength-=graph[cur][i].length; curTime-=graph[cur][i].time; isReached[i]=false; } } } int main() { int N,M; scanf("%d%d",&N,&M); while(M--) { int v1,v2,one_way,length,time; scanf("%d%d%d%d%d",&v1,&v2,&one_way,&length,&time); graph[v1][v2].length=length; graph[v1][v2].time=time; if(one_way==0) { graph[v2][v1].length=length; graph[v2][v1].time=time; } } int start,end; scanf("%d%d",&start,&end); DFS(start,end); if(minTRoad!=minLRoad) { cout<<"Distance = "<<minLength<<": "<<start; for(int i=0;i<minLRoad.size();i++) { cout<<" -> "<<minLRoad[i]; } cout<<endl; cout<<"Time = "<<minTime<<": "<<start; for(int i=0;i<minTRoad.size();i++) { cout<<" -> "<<minTRoad[i]; } } else { cout<<"Distance = "<<minLength<<"; "; cout<<"Time = "<<minTime<<": "<<start; for(int i=0;i<minTRoad.size();i++) { cout<<" -> "<<minTRoad[i]; } } return 0; }
相关文章推荐
- C#学习-----简单程序编写
- 近期最久未使用页面淘汰算法———LRU算法(java实现)
- 七年阿里老人谈新人成长
- 15.linux按键驱动程序(二)
- 游戏开发数值原理与技巧 (14)
- js实现表单联动
- [BZOJ2005][NOI2010]能量采集(莫比乌斯反演)
- lsm-tree
- struct内存对齐
- POJ 2777(线段树,区间更新,使用二进制染色问题)
- 价值观作业
- 配置git时出现permission denied的一个解决方法
- 理解Windows内核模式与用户模式
- FILE文件流的中对fopen、fseek、fread、fwrite,fprintf、fputs、fgets的理解
- maven web项目pom.xml配置
- uva 11396 Claw Decomposition(二分图)
- 构建之法阅读笔记06
- 第一行代码读书规划
- 【坑】tomcat中上传的图片文件无故消失
- LeeCode(Database)-Customers Who Never Order