poj1724

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35356190/article/details/70182981
#include<iostream>
#include<vector> 
#include<cstring>
using namespace std;


struct Road{
int aim,money,length;
};


vector< vector< Road> > g(110);
//共有多少个城市 
int N=0;
// 共有多少钱 
int M=0;
// 共有多少条路 
int R=0;
int visited[111];
int totalMoney=0;
int minLength=1<<30;
int totalLength=0;
//定义数组,保存中间结果
int minL[110][10010]; 




void dfs(int x){
if(x==N){
if(totalLength<minLength){
minLength=totalLength;
}
}
for(int i=0;i<g[x].size();++i){
Road r=g[x][i];
if(totalMoney+r.money>M)continue;

if(!visited[r.aim]){
if(totalLength+r.length>=minLength)continue;
if(totalLength+r.length>=minL[r.aim][totalMoney+r.money])continue;
minL[r.aim][totalMoney+r.money]=totalLength+r.length;
visited[r.aim]=1;
totalLength+=r.length;
totalMoney+=r.money; 
dfs(r.aim);
totalLength -= r.length;
totalMoney  -= r.money; 
visited[r.aim]=0;
}

}
}


int main(){
cin>>N>>M>>R;
for(int i=0;i<R;++i){
int s;
Road r;
cin>>s>>r.aim>>r.money>>r.length;
if(r.aim!=s){
g[s].push_back(r);
}
}

for(int i=0;i<110;++i){
for(int j=0;j<10010;++j){
minL[i][j]=1<<30;
}
}
memset(visited,0,sizeof(visited));
visited[1]=1;
dfs(1);
if(minLength<1<<30){
cout<<minLength<<endl;
}else{
cout<<"-1"<<endl;
}

return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_35356190/article/details/70182981