poj 1062(最短路)
此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等级限制为2,那么需要枚举等级从3~5,4~6,5~7.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<stack> 8 #include<string> 9 #include<vector> 10 #include<cstdlib> 11 #include<map> 12 #include<set> 13 using namespace std; 14 #define CL(x,v) memset(x,v,sizeof(x)); 15 #define R(i,st,en) for(int i=st;i<en;++i) 16 #define LL long long 17 #define inf 0x3f3f3f3f 18 19 const int maxn = 105; 20 int m, n; 21 struct node 22 { 23 int p; 24 int le; 25 }goods[maxn]; 26 struct edge 27 { 28 int v; 29 int c; 30 edge(){} 31 edge(int v, int c):v(v),c(c){} 32 }; 33 vector <edge> adj[maxn]; 34 int LP,maxLe; 35 bool vis[maxn]; 36 int dis[maxn]; 37 int dij() 38 { 39 CL(dis,0x37); 40 dis[1] = 0; 41 for (int i = 0; i < adj[1].size(); ++ i) 42 { 43 if(!vis[adj[1][i].v]) 44 dis[adj[1][i].v] = adj[1][i].c; 45 } 46 for (int i = 1 ; i < n; ++ i) 47 { 48 int tmp = inf, k = 1; 49 for (int j = 1; j <= n; ++ j) 50 { 51 if(!vis[j] && (tmp > dis[j])) 52 { 53 tmp = dis[j]; 54 k = j; 55 } 56 } 57 vis[k] = 1; 58 for (int j = 0; j < adj[k].size(); ++ j) 59 { 60 if(!vis[adj[k][j].v]) 61 dis[adj[k][j].v] = min(dis[adj[k][j].v], dis[k] + adj[k][j].c); 62 } 63 } 64 int minans = inf; 65 for (int i = 1; i <= n; ++ i) 66 { 67 if(minans > dis[i] + goods[i].p) 68 minans = dis[i] + goods[i].p; 69 } 70 return minans; 71 } 72 int main() 73 { 74 while(~scanf("%d%d",&m, &n)) 75 { 76 CL(goods,0); 77 CL(adj,0); 78 maxLe = 0; 79 R(i, 1, n+1) 80 { 81 int x, v, c; 82 scanf("%d%d%d", &goods[i].p, &goods[i].le, &x); 83 if(goods[i].le > maxLe) 84 maxLe = goods[i].le; 85 while(x --) 86 { 87 scanf("%d%d", &v, &c); 88 adj[i].push_back(edge(v,c)); 89 } 90 } 91 LP = goods[1].le; 92 int st = (LP - m) > 0 ? (LP - m) : 0; 93 int ans = inf; 94 for (int i = st; i <= LP; ++ i) 95 { 96 CL(vis,0); 97 for (int j = 1; j <= n; ++ j) 98 { 99 if(goods[j].le < i || abs(goods[j].le - i) > m) 100 vis[j] = 1; 101 } 102 ans = min(ans, dij()); 103 } 104 printf("%d\n",ans); 105 } 106 return 0; 107 }