iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练。
通过了一周理论知识和一周基本魔法的学习以后,iPig对猪世界的世界本原有了不少的了解:众所周知,世界是由元素构成的;元素与元素之间能够互相转换;能量守恒……。
iPig 今天就在进行一个麻烦的测验。
iPig 在以前的学习中已经知道了不少种元素,并学会了能够转化这些元素的魔法,每种魔法须要消耗 iPig 必定的能量。
做为 PKU 的顶尖学猪,让 iPig 用最少的能量完成从一种元素转换到另外一种元素……等等,iPig 的魔法导猪可没这么笨!
这一次,他给 iPig 带来了不少 1 号元素的样本,要求 iPig 使用学习过的魔法将它们一个个转化为 N 号元素,为了增长难度,要求每份样本的转换过程都不相同。
这个看似困难的任务实际上对 iPig 并无挑战性,由于,他有坚实的后盾……如今的你呀!
注意,两个元素之间的转化可能有多种魔法,转化是单向的。
转化的过程当中,能够转化到一个元素(包括开始元素)屡次,可是一但转化到目标元素,则一份样本的转化过程结束。
iPig 的总能量是有限的,因此最多可以转换的样本数必定是一个有限数。具体请参看样例。 php
占总分不小于 10% 的数据知足 N <= 6,M<=15。
占总分不小于 20% 的数据知足 N <= 100,M<=300,E<=100且E和全部的ei均为整数(能够直接做为整型数字读入)。
全部数据知足 2 <= N <= 5000,1 <= M <= 200000,1<=E<=107,1<=ei<=E,E和全部的ei为实数。
ios
询问第一个知足1~k短路的和>E的k。ide
求k短路,直接运用A*搜索便可,把T->每一个点的最短路当作估价便可。学习
1 #include<iostream>
2 #include<string>
3 #include<algorithm>
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 #include<cmath>
8 #include<queue>
9 using namespace std; 10 typedef long long s64; 11
12 const int ONE = 205000; 13 const int POI = 5500; 14 const double INF = 1e18; 15
16 int n,m; 17 int S,T; 18 double dist[POI],w[ONE],E; 19 bool vis[POI]; 20 int next[ONE],first[POI],go[ONE],tot; 21 int Ans; 22
23 struct point 24 { 25 int x,y; double z; 26 }a[ONE]; 27
28 struct power 29 { 30 int x; double real; 31 bool operator <(const power &a) const
32 { 33 return a.real + dist[a.x] < real + dist[x]; 34 } 35 }; 36
37 inline int get() 38 { 39 int res=1,Q=1; char c; 40 while( (c=getchar())<48 || c>57) 41 if(c=='-')Q=-1; 42 if(Q) res=c-48; 43 while((c=getchar())>=48 && c<=57) 44 res=res*10+c-48; 45 return res*Q; 46 } 47
48 void Add(int u,int v,double z) 49 { 50 next[++tot]=first[u]; first[u]=tot; go[tot]=v; w[tot]=z; 51 } 52
53 void SPFA(int x) 54 { 55 queue <int> q; 56 q.push(x); 57 for(int i=S;i<=T;i++) dist[i] = INF; 58 vis[x] = 1; dist[x] = 0; 59 while(!q.empty()) 60 { 61 int u = q.front(); q.pop(); 62 for(int e=first[u];e;e=next[e]) 63 { 64 int v = go[e]; 65 if(dist[v] > dist[u] + w[e]) 66 { 67 dist[v] = dist[u] + w[e]; 68 if(!vis[v]) vis[v] = 1, q.push(v); 69 } 70 } 71 vis[u] = 0; 72 } 73 } 74
75 void Astar() 76 { 77 priority_queue <power> q; 78 q.push( (power){S, 0} ); 79 while(!q.empty()) 80 { 81 power u = q.top(); q.pop(); 82 if(u.x == T) {E -= u.real; if(E < 0) return; Ans++;} 83 if(u.real + dist[u.x] > E) continue; 84 for(int e=first[u.x]; e; e=next[e]) 85 q.push( (power){go[e], u.real+w[e]} ); 86
87 } 88 } 89
90 int main() 91 { 92 n=get(); m=get(); scanf("%lf",&E); 93 S=1, T=n; 94 for(int i=1;i<=m;i++) 95 { 96 a[i].x=get(); a[i].y=get(); scanf("%lf",&a[i].z); 97 Add(a[i].y, a[i].x, a[i].z); 98 } 99 SPFA(T); 100
101 memset(first,0,sizeof(first)); tot=0; 102 for(int i=1;i<=m;i++) Add(a[i].x,a[i].y,a[i].z); 103
104 Astar(); 105
106 printf("%d",Ans); 107 }