ROADS ----- 深度优先搜索
来源:互联网 发布:私人网络摄像头直播 编辑:程序博客网 时间:2024/05/09 17:55
Memory Limit: 65536KTotal Submissions: 10335
Accepted: 3819
Description
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
Input
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
- S is the source city, 1 <= S <= N
- D is the destination city, 1 <= D <= N
- L is the road length, 1 <= L <= 100
- T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
Output
If such path does not exist, only number -1 should be written to the output.
Sample Input
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2
Sample Output
11
Source
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int K,N,R,S,D,L,T;
struct Road{
int d,L,t;
};
vector< vector<Road> >cityMap(110);
int minLen=1<<30;
int totalLen;
int totalCost;
int visited[110];
int minL[110][10100];
void Dfs(int s)
{
if(s==N)
{
minLen=min(minLen,totalLen);
return;
}
for(int i=0;i<cityMap[s].size();++i)
{
int d=cityMap[s][i].d;
if(!visited[d])
{
int cost=totalCost+cityMap[s][i].t;
if(cost>K)
continue;
if(totalLen+cityMap[s][i].L>=minLen || totalLen + cityMap[s][i].L>=minL[d][cost])
continue;
totalLen+=cityMap[s][i].L;
totalCost+=cityMap[s][i].t;
minL[d][cost]=totalLen;
visited[d]=-1;
Dfs(d);
visited[d]=0;
totalCost-=cityMap[s][i].t;
totalLen-=cityMap[s][i].L;
}
}
}
int main()
{
cin>>K>>N>>R;
for(int i=0;i<R;i++)
{
int s;
Road r;
cin>>s>>r.d>>r.L>>r.t;
if(s!=r.d)
{
cityMap[s].push_back(r);
}
}
for(int i=0;i<110;i++)
{
for(int j=0;j<10100;j++)
{
minL[i][j]=1<<30;
}
}
memset(visited,0,sizeof(visited));
totalLen=0;
totalCost=0;
visited[1]=1;
minLen=1<<30;
Dfs(1);
if(minLen<(1<<30))
{
cout<<minLen<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
- ROADS ----- 深度优先搜索
- 深度优先搜索
- 深度优先搜索
- 深度优先搜索 DFS
- 深度优先搜索遍历
- 深度优先搜索 DFS
- 深度优先搜索
- 深度优先搜索
- 深度优先搜索算法
- hdoj1015Safecracker(深度优先搜索)
- [AI]深度优先搜索
- 深度优先搜索算法
- DFS 深度优先搜索
- 深度优先搜索
- 深度优先搜索算法
- 图解深度优先搜索
- 深度优先搜索
- 深度优先搜索
- DSP仿真器的问题
- Shell编程示例
- ios7新增基础类库以及OC新特性
- android 调用系统相机拍照 获取原图
- 关于招标说明书的撰写要点
- ROADS ----- 深度优先搜索
- Oracle 经典SQL 专为笔试准备
- 用java的swing写了个图片标注工具
- SqlBulkCopy批量插入数据
- PHP----读(配置)ini文件
- 关于PHP内存溢出问题的解决方法
- Node.js入门笔记
- try catch finally 和 return 之间的顺序纠葛
- 设计师整理的系统开发流程-简洁又有重点