这个题表述的有点模糊。我一开始认为只要到公主的房间就停止了,即只有在最后一步进到公主的房间,在中间过程是不能经过公主的房间。 但是一直wa,所以我就把数据下载下来,跑了几个wa的数据发现,原来中间过程可以经过公主的房间。 另外,此题说随意给出一组答案就行,但是我通过跑测试数据发现我的答案是对的,但是就是wa,估计系统检测有问题,希望大家做的时候注意此题。下载数据花了我100积分。。我在此共享给大家。
我的代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
int n,m,e,p,b;
int cost[105],map[105][105],dp[105][1005];
struct Node{
int num,money;
};
queue<Node> q;
void init(){
cin >> n >> m >> e >> p >> b;
memset(map, 0, sizeof(map));
for(int i = 1; i <= n; i++) cin >> cost[i];
for(int i = 0; i < m; i++){
int x,y;
cin >> x >> y;
map[x][y] = 1;
map[y][x] = 1;
}
memset(dp, 0, sizeof(dp));
}
int main(int argc, const char * argv[])
{
init();
if(p==e) cout << p << endl;
else{
Node node = Node{e,b-cost[e]};
q.push(node);
while(!q.empty() && !dp[p][0]){
Node temp = q.front();
q.pop();
for(int i = 1; i <= n; i++){
if(map[temp.num][i]){
if(cost[i]<=temp.money && !dp[i][temp.money-cost[i]]){
q.push(Node{i,temp.money-cost[i]});
dp[i][temp.money-cost[i]] = temp.num;
}
}
}
}
vector<int> result;
int sum = cost[p];
int t = dp[p][0];
result.push_back(t);
while(!(t==e&&sum==b-cost[e])){
int temp = t;
t = dp[t][sum];
result.push_back(t);
sum += cost[temp];
}
for(int i = result.size()-1; i>=0; i--) printf("%d ",result[i]);
cout << p << endl;
}
}