标签:
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
/* 用物品为点建图,枚举交易范围,spfa */ #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<queue> #define ll long long using namespace std; const int maxn = 20005; const ll inf = 987654321234LL; int read(){ char ch=getchar(); int x=0,f=1; while(!(ch>=‘0‘&&ch<=‘9‘)){if(ch==‘-‘)f=-1;ch=getchar();}; while(ch>=‘0‘&&ch<=‘9‘){x=x*10+(ch-‘0‘);ch=getchar();}; return x*f; } struct edge{ int v; int w; int nxt; }e[maxn*3]; int n,m; int cnt,head[maxn]; int p[maxn],l[maxn],sz[maxn]; int t,v; int flag,vis[maxn]; ll dis[maxn],ans; void ins(int u,int v,int w){ cnt++; e[cnt].v = v; e[cnt].w = w; e[cnt].nxt = head[u]; head[u] = cnt; } void spfa(int t){ flag++; dis[0] = 0; for(int i = 1;i <= n;i++) dis[i] = inf; queue<int> q; vis[0] = flag; q.push(0); int now,to; while(!q.empty()){ now = q.front(); q.pop(); for(int i = head[now];i;i = e[i].nxt){ to = e[i].v; if(dis[to] > dis[now] + e[i].w && l[to] >= t && l[to] <= t+m){ dis[to] = dis[now] + e[i].w; if(vis[to] != flag){ vis[to] = flag; q.push(to); } } } vis[now] = false; } ans = min(ans,dis[1]); } int main(){ m = read(); n = read(); for(int i = 1;i <= n;i++){ p[i] = read(); l[i] = read(); sz[i] = read(); ins(0,i,p[i]); for(int j = 1;j <= sz[i];j++){ t = read(); v = read(); ins(t,i,v); } } ans = inf; for(int i = l[1] - m;i <= l[1];i++) spfa(i); cout<<ans; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5920980.html