Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11265 | Accepted: 4172 |
Description
Input
Output
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <list> 13 #include <iomanip> 14 #include <cstdlib> 15 #include <sstream> 16 using namespace std; 17 typedef long long LL; 18 const int INF=0x5fffffff; 19 const double EXP=1e-6; 20 const int MS1=105; 21 const int MS2=10005; 22 23 struct edge 24 { 25 int t,len,money; 26 }; 27 28 vector<vector<edge> > road(MS1); 29 int vis[MS1]; 30 int ans; 31 int len; 32 int money; 33 int d[MS1][MS2]; 34 int K,N,R; 35 36 void dfs(int cur) 37 { 38 if(cur==N) 39 { 40 ans=min(ans,len); 41 return ; 42 } 43 for(int i=0;i<road[cur].size();i++) 44 { 45 int t=road[cur][i].t; 46 if(!vis[t]) 47 { 48 int cash=road[cur][i].money; 49 int l=road[cur][i].len; 50 if(money+cash>K||len+l>ans) 51 continue; 52 if(len+l>d[t][money+cash]) 53 continue; 54 vis[t]=1; 55 len+=l; 56 money+=cash; 57 d[t][money]=len; 58 dfs(t); 59 vis[t]=0; 60 len-=l; 61 money-=cash; 62 } 63 } 64 } 65 66 int main() 67 { 68 scanf("%d%d%d",&K,&N,&R); 69 int s; 70 edge e; 71 for(int i=0;i<R;i++) 72 { 73 scanf("%d%d%d%d",&s,&e.t,&e.len,&e.money); 74 road[s].push_back(e); 75 } 76 memset(vis,0,sizeof(vis)); 77 for(int i=0;i<MS1;i++) 78 for(int j=0;j<MS2;j++) 79 d[i][j]=INF; 80 vis[1]=1; 81 ans=INF; 82 len=0; 83 money=0; 84 dfs(1); 85 if(ans==INF) 86 printf("-1\n"); 87 else 88 printf("%d\n",ans); 89 return 0; 90 }
原文:http://www.cnblogs.com/767355675hutaishi/p/4361180.html