题意:每走一段路可以获得10根木头,问从S到T至少要获得K根木头,,,问要需要走多少路程,。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int N = 5009; const int M = 200009; const int INF = 0x3f3f3f3f; int n,m; int S,T,K; struct LT{ int nex,to,dis; } L[M]; int F[N],cnt; void add(int f,int t,int d) { L[cnt].dis = d; L[cnt].nex = F[f]; L[cnt].to = t; F[f] =cnt++; } int dis[N][59]; void init() { memset(F,0,sizeof(F));cnt =1; int f,t,d; for(int i=0;i<m;i++) scanf("%d%d%d",&f,&t,&d),add(f,t,d),add(t,f,d); scanf("%d%d%d",&S,&T,&K); K = (K+9)/10; } struct node{ int v,h,dis; bool operator<(const node t) const { return dis>t.dis; } }; priority_queue<node> que; bool visit[N][59]; void solve() { while(!que.empty()) que.pop(); node e,t; memset(dis,INF,sizeof(dis)); memset(visit,false,sizeof(visit)); e.dis = 0,e.h=0,e.v=S; que.push(e); dis[S][0] = 0; while(!que.empty()) { e =que.top();que.pop(); //cout<<e.v<<" "<<e.h<<" "<<e.dis<<endl; if(e.v==T&&e.h>=K) { printf("%d\n",e.dis); return ; } if(e.h>55||visit[e.v][e.h]) continue; visit[e.v][e.h] = true; for(int i=F[e.v];i;i=L[i].nex) { int to = L[i].to; if(dis[to][e.h+1]>e.dis+L[i].dis) { t.dis=e.dis+L[i].dis; dis[to][e.h+1]=t.dis; t.h = e.h+1; t.v=to; que.push(t); } } } printf("-1\n"); } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { //cout<<n<<" - "<<m<<endl; init(); solve(); //cout<<"Solve"<<endl; } return 0; }