昂贵的聘礼
64-bit integer IO format: %lld Java class name: Main
为了方便起见,我们把所有的物品从1开始进行编号,酋长的允诺也看作一个物品,并且编号总是1。每个物品都有对应的价格P,主人的地位等级L,以及一系列的替代品Ti和该替代品所对应的"优惠"Vi。如果两人地位等级差距超过了M,就不能"间接交易"。你必须根据这些数据来计算出探险家最少需要多少金币才能娶到酋长的女儿。
Input
Output
Sample Input
1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0
Sample Output
5250
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 #define pii pair<int,int> 16 using namespace std; 17 const int maxn = 110; 18 struct arc{ 19 int to,cost,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 cost = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*maxn]; 27 int head[maxn],d[maxn],lv[maxn]; 28 int N,M,P,L,X,T,V,tot; 29 bool done[maxn]; 30 void add(int u,int v,int cost){ 31 e[tot] = arc(v,cost,head[u]); 32 head[u] = tot++; 33 } 34 void dijkstra(int low,int high){ 35 for(int i = 0; i <= N; ++i){ 36 d[i] = INF; 37 done[i] = false; 38 } 39 d[0] = 0; 40 priority_queue< pii,vector< pii >,greater< pii > >q; 41 q.push(make_pair(0,0)); 42 while(!q.empty()){ 43 int u = q.top().second; 44 q.pop(); 45 if(done[u]) continue; 46 done[u] = true; 47 for(int i = head[u]; ~i; i = e[i].next){ 48 if(lv[e[i].to] >= low && lv[e[i].to] <= high && d[e[i].to] > d[u] + e[i].cost){ 49 d[e[i].to] = d[u] + e[i].cost; 50 q.push(make_pair(d[e[i].to],e[i].to)); 51 } 52 } 53 if(done[1]) return; 54 } 55 } 56 int main(){ 57 while(~scanf("%d %d",&M,&N)){ 58 memset(head,-1,sizeof(head)); 59 tot = 0; 60 for(int i = 1; i <= N; ++i){ 61 scanf("%d %d %d",&P,&L,&X); 62 add(0,i,P); 63 lv[i] = L; 64 while(X--){ 65 scanf("%d %d",&T,&V); 66 add(T,i,V); 67 } 68 } 69 int ans = INF; 70 for(int i = lv[1] - M; i <= lv[1]; ++i){ 71 dijkstra(i,i+M); 72 ans = min(ans,d[1]); 73 } 74 printf("%d\n",ans); 75 } 76 return 0; 77 }