标签:des style class blog code http
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35515 | Accepted: 10163 |
Description
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
思路:
最短路的应用,我的思路是从酋长处开始,求到每个点的最短路,然后加上每个点的val 就是此点的最短路。 难点在于枚举m次(m为输入的等级限制)。
但是我的wrong点不是这个。在代码中注释我错误的地方。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : 昂贵的聘礼.cpp 4 * Creat time : 2014-06-10 19:49 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 110 15 #define INF 0x7f7f7f7f 16 using namespace std; 17 int c[M][M],dis[M],v[M],level[M],n,isin[M]; 18 bool vis[M]; 19 int dijkstra() 20 { 21 int i,j,k; 22 int ans = INF,_min; 23 clr(vis,0); 24 for(i = 2; i <= n; i++){ 25 dis[i] = c[1][i]; 26 } 27 dis[1] = 0; 28 vis[1] = true; 29 for(i = 1; i <= n; i++){ 30 _min = INF; 31 j = 0; 32 for(k = 1; k <= n; k++){ 33 if(_min > dis[k] && !vis[k] && isin[k]){ 34 _min = dis[k]; 35 j = k; 36 } 37 } 38 vis[j] = true; 39 for(k = 1; k <= n; k++){ 40 if(!vis[k] && isin[k] && dis[k] > dis[j] + c[j][k]){ 41 dis[k] = dis[j] + c[j][k]; 42 } 43 } 44 } 45 for(i = 1; i <= n; i++){ 46 dis[i] += v[i]; 47 if(dis[i] < ans && vis[i]){ //关键在于判断是否访问过,如果无,则错误。下面代码给出另一种方法,不同之处是将dis数组初始化为INF。 48 ans = dis[i]; 49 } 50 } 51 return ans; 52 } 53 int main(int argc,char *argv[]) 54 { 55 int t,lev,num,val; 56 while(scanf("%d%d",&lev,&n)!=EOF){ 57 for(int i = 0; i <= n; i++){ 58 for(int j = i; j <= n; j++){ 59 c[i][j] = c[j][i] = INF; 60 if(i == j) c[i][i] = 0; 61 } 62 } 63 for(int i = 1; i <= n; i++){ 64 scanf("%d%d%d",&v[i],&level[i],&t); 65 for(int j = 1; j <= t; j++){ 66 scanf("%d%d",&num,&val); 67 c[i][num] = val; 68 } 69 } 70 int ans = INF; 71 for(int i = 0; i <= lev; i++){ 72 clr(isin,0); 73 for(int j = 1; j <= n; j++){ 74 if(level[j] >= level[1]-lev+i && level[j] <= level[1]+i){ 75 isin[j] = 1; 76 } 77 } 78 t = dijkstra(); 79 if(ans > t) 80 ans = t; 81 } 82 printf("%d\n",ans); 83 } 84 return 0; 85 }
1 /*====================================================================== 2 * Author : kevin 3 * Filename : 昂贵的聘礼.cpp 4 * Creat time : 2014-06-10 19:49 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 110 15 #define INF 0x7f7f7f7f 16 using namespace std; 17 int c[M][M],dis[M],v[M],level[M],n,isin[M]; 18 bool vis[M]; 19 int dijkstra() 20 { 21 int i,j,k; 22 int ans = INF,_min; 23 clr(vis,0); 24 for(i = 2; i <= n; i++){ 25 dis[i] = INF; // 不同之处。。。 26 } 27 dis[1] = 0; 28 //vis[1] = true; 29 for(i = 1; i <= n; i++){ 30 _min = INF; 31 j = 0; 32 for(k = 1; k <= n; k++){ 33 if(_min > dis[k] && !vis[k] && isin[k]){ 34 _min = dis[k]; 35 j = k; 36 } 37 } 38 vis[j] = true; 39 for(k = 1; k <= n; k++){ 40 if(isin[k] && dis[k] > dis[j] + c[j][k]){ 41 dis[k] = dis[j] + c[j][k]; 42 } 43 } 44 } 45 for(i = 1; i <= n; i++){ 46 dis[i] += v[i]; 47 if(dis[i] < ans){ 48 ans = dis[i]; 49 } 50 } 51 return ans; 52 } 53 int main(int argc,char *argv[]) 54 { 55 int t,lev,num,val; 56 while(scanf("%d%d",&lev,&n)!=EOF){ 57 for(int i = 0; i <= n; i++){ 58 for(int j = i; j <= n; j++){ 59 c[i][j] = c[j][i] = INF; 60 if(i == j) c[i][i] = 0; 61 } 62 } 63 for(int i = 1; i <= n; i++){ 64 scanf("%d%d%d",&v[i],&level[i],&t); 65 for(int j = 1; j <= t; j++){ 66 scanf("%d%d",&num,&val); 67 c[i][num] = val; 68 } 69 } 70 int ans = INF; 71 for(int i = 0; i <= lev; i++){ 72 clr(isin,0); 73 for(int j = 1; j <= n; j++){ 74 if(level[j] >= level[1]-lev+i && level[j] <= level[1]+i){ 75 isin[j] = 1; 76 } 77 } 78 t = dijkstra(); 79 if(ans > t) 80 ans = t; 81 } 82 printf("%d\n",ans); 83 } 84 return 0; 85 }
下面给出几组测试数据,有些是在discuss中出现的,借鉴一下。
1 5
10000 3 3
2 8000
3 5000
5 1000
1000 2 1
4 200
3000 2 1
4 200
50 2 0
50 6 0
5250
wrong ans:1050
测试数据1: 1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0 5250 测试数据2: 1 5 10000 3 4 2 3000 3 2000 4 2000 5 9000 8000 2 3 3 5000 4 2000 5 7000 5000 1 0 2000 4 1 5 1900 50 1 0 4000 测试数据3: 3 8 10000 3 6 2 3000 3 2000 4 2000 5 9000 7 1000 8 5008 8000 2 3 3 5000 4 2000 5 7000 5000 1 1 6 1000 2000 4 1 5 1900 50 1 0 5000 1 1 7 4007 2000 4 1 5 1900 80 3 0 2950 测试数据4: 1 10 1324 0 0 1234 0 0 255 0 0 67 0 0 56 0 0 2134 0 0 456 0 0 2345 0 0 67 0 0 6436 0 0 1324 测试数据5: 1 4 10000 3 2 2 1 3 3 1000 2 2 4 1 3 1 1000 3 1 4 2 100 4 0 105 测试数据6: 3 5 10000 3 4 2 3000 3 2000 4 2000 5 9000 8000 2 3 3 5000 4 2000 5 7000 5000 1 0 2000 4 1 5 1900 50 1 0 3950 测试数据7: 0 5 10000 3 4 2 3000 3 2000 4 2000 5 9000 8000 2 3 3 5000 4 2000 5 7000 5000 4 0 2000 3 1 5 1900 50 2 0 4000
poj 1062 -- 昂贵的聘礼,布布扣,bubuko.com
标签:des style class blog code http
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3789223.html