标签:
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
Source
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f, MAX = 200; int map[MAX][MAX],p[MAX],d[MAX],rank[MAX];//map为交换所需费用,p为该物品的价值,d为得到该物品所需要的最小花费 bool vis[MAX];//vis用来标记是否可行,且执行过之后的d不能再执行 int m, n, x; int t, v; void dijkstra(int l, int r) { memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++){ if(rank[i] < l||rank[i] > r) vis[i] = 1; d[i] = inf; } d[1] = 0; for(int i = 1; i <= n; i++){ int minc = inf,k; for(int j = 1; j <= n; j++){ if(!vis[j]&& d[j] < minc){ minc = d[j]; k = j; } }//若果d[j]可以交换的话,就去最小的 vis[k] = 1; for(int j = 1; j <= n ; j++){ if(!vis[j] && d[j] > d[k] + map[k][j]) d[j] = d[k] + map[k][j]; } } } int main() { int ans; while(~scanf("%d%d",&m,&n)){ for(int i = 1; i <= n; i++) for(int j = 1;j <= n ;j++) map[i][j] = inf; for(int i = 1; i <= n; i++){ scanf("%d%d%d",&p[i],&rank[i],&x); while(x--){ scanf("%d%d",&t,&v); map[i][t] = v; } } ans = inf; for(int i = rank[1] - m; i<= rank[1];i++){ dijkstra(i,i+m); for(int j = 1; j <= n ;j++) ans = min(ans,p[j]+d[j]); } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4319208.html