标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 44870 | Accepted: 13259 |
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 <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const int INF = 999999999; const int N = 105; int m,n; int MIN; int graph[N][N]; struct Node{ int v,level; }node[105]; bool vis[N]; void dfs(int u,int ans,int level1,int level2){ vis[u] = true; MIN = min(ans+node[u].v,MIN); for(int i=1;i<=n;i++){ if(!vis[i]&&graph[u][i]<INF){ if(abs(node[i].level-level1)>m) continue; if(abs(node[i].level-level2)>m) continue; dfs(i,ans+graph[u][i],min(node[i].level,level1),max(level2,node[i].level)); vis[i] = false; } } } int main() { while(scanf("%d%d",&m,&n)!=EOF){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ graph[i][j] = INF; if(i==j) graph[i][j] = 0; } } int LEVEL; for(int i=1;i<=n;i++){ int num; scanf("%d%d%d",&node[i].v,&node[i].level,&num); for(int j=1;j<=num;j++){ int a,b; scanf("%d%d",&a,&b); graph[i][a] = min(b,graph[i][a]); } } LEVEL = node[1].level; memset(vis,0,sizeof(vis)); MIN = 99999999999; dfs(1,0,LEVEL,LEVEL); printf("%d\n",MIN); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5689696.html