原题及翻译
ROADS
路
Time Limit: 1000MS
时间限制:1000ms
Memory Limit: 65536K
内存限制:65536k
Total Submissions: 19213
总提交:19213
Accepted: 6804
通过:6804
Description
描述
N cities named with numbers 1 … N are connected with one-way roads.
N个城市,分别是1……N它们之间有单向连通的道路。
Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
每路有两个参数:长度与它的过路费(支付需要的是那条路的硬币中的数量)。
Bob and Alice used to live in the city 1.
Bob和Alice过去常常住在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.
在他注意到爱丽丝喜欢卡片游戏后,鲍勃决定了去城市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.
我们要找到最短的路径来帮助鲍勃从1城市到N城市,并且以他能负担得起一个金额。
Input
输入
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
第一行输入线的整数k,0≤k≤10000,是鲍勃所有的钱。
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
第二行是整数n,2≤N≤100,总数量的城市。
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
第三行是整数R,1≤R≤10000,总数量的道路。
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
每个R线以下介绍一道由指定的单空白字符分隔的D,L和S,T:
S is the source city, 1 <= S <= N
S是出发城市,1 <= S <= N
D is the destination city, 1 <= D <= N
D是目的城市,1 <= D <= N
L is the road length, 1 <= L <= 100
L是道长,1 <= L <= 100
T is the toll (expressed in the number of coins), 0 <= T <=100
T是收费(以硬币的数量表示),0 <= T <=100
Notice that different roads may have the same source and destination cities.
注意,不同的道路可能有相同的起点和目的地城市。
Output
输出
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
只有一个输出,应包含最短路径的总长度从1到n的最大城市,总金额是小于或等于k的硬币。
If such path does not exist, only number -1 should be written to the output.
如果这样的路径不存在,输出数字-1。
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
11
Source
CEOI 1998
解题思路
从城市1开始深度优先遍历整个图,找到所有能过到达N的走法,选一个最优的。
但是,直接这样遍历是会超时的,这就需要对程序进行优化剪枝。
最优性剪枝:
1.如果当前已经找到的最优路径程度为L,那么在继续搜索的过程中,总长度已经大于等于L的走法就可以直接放弃,不用走到底了。
保存中间值的最优性剪枝。
2.用mid[k][m]表示:走到城市k时总过路费为m的条件下,最优路径的长度。若在后续的搜索中,再次走到k时,如果总路费恰好为m,且此时的最优长度已经超过mid[k][m],则不必再走下去了。
代码分析
1.可以用链接表存储城市的数据,先声明一个城市道路的结构,然后创建一个Road类型的链接表cityMap:
struct Road
{
int d,L,t;
};
vector<vector<Road> > cityMap(110);
2.然后创建需要的几个全局变量(表示当前找到的最优路径长度minLen,初始化为一个很大的值、表示正在走的路径的长度totalLen、表示走过路径的总花销totalCost、用于标记城市是否走过的数组visited[110]、还有用于表示从1到某个城市最短路径的长度的花销minL[110][10100]):
int K,N,R;
int minLen = 1 << 30;
int totalLen=0;
int totalCost=0;
int visited[110];
int minL[110][10100];
3.接下来写主函数,用于读入和处理数据以及调用函数还有结果的输出:
int main()
{
cin >>K >> N >> R;
//读入K(鲍勃所有的钱),N(城市的总数量),R(道路的总数量)
for( int i = 0;i < R; ++ i)
{
int s;
//城市的序号
Road r;
//Road类型的变量r
cin >> s >> r.d >> r.L >> r.t;
//读入出发城市的序号、目的城市的序号、道长和过路费。
if( s != r.d )
//排除只有一个城市的情况
cityMap[s].push_back(r);
//将r添加到cityMap中
}
for( int i = 0;i < 110; ++i )
for( int j = 0; j < 10100; ++ j )
minL[i][j] = 1 << 30;
memset(visited,0,sizeof(visited));
//先将所有的城市都标记为0
visited[1] = 1;
//然后把第一个城市标记为1
Dfs(1);
//调用深度搜索函数
if( minLen < (1 << 30))
//如果有比极大值小的路径则输出
cout << minLen << endl;
else
//如果没有则输出-1
cout << "-1" << endl;
}
4.最后就是主角登场了,深度搜索函数:
void Dfs(int s)
{//从s开始向N行走
if(s==N)
{//如果到达终点,返回当前总路径和最短路径。
minLen = min(minLen,totalLen);
return ;
}
for(int i=0;i<cityMap[s].size();++i )
{//遍历cityMap的所有元素
int d=cityMap[s][i].d;
//s有路连到d
if(! visited[d] )
{//确定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;
//累加所有的数据并标价城市为1
Dfs(d);
//从当前位置出发往N走
visited[d]=0;
totalCost-=cityMap[s][i].t;
totalLen-=cityMap[s][i].L;
//如果不能到达N或者条件不允许,退回。
}
}
}
完整代码
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int K,N,R;
struct Road
{
int d,L,t;
};
vector<vector<Road> > cityMap(110);
int minLen=1<<30;
int totalLen=0;
int totalCost=0;
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));
visited[1] = 1;
minLen = 1 << 30;
Dfs(1);
if( minLen < (1 << 30))
cout << minLen << endl;
else
cout << "-1" << endl;
}