topic
Description [title]
[Input Format]
[Output format]
[Sample]
Sample input
5 5 10 8 1 2 1 1 3 3 1 4 2 2 3 2 2 4 4 3 4 1 3 5 2 4 5 2 4 2 2 3 3 1 1 3 3 3 4 4 5
Sample Output
32
Sample [explain]
![](https://img2018.cnblogs.com/blog/1731121/201910/1731121-20191011133521575-1386336442.png)
answer
[Managers] do title
First of all, this question involves a short circuit is most definitely see that.
But then, in the end how to deal with the situation can not be some way to go yet?
This time, I think the most short-circuit and short-circuits, it is processed in a similar way to the tree $ dp $.
But so want to have a $ bug $, that is the situation in case of the most short-circuit and short-circuits are dead end it?
Then I started crazy fantasy times and short-circuit, short-circuit times and times ......
[Correct]
I'm considering this question, when this question ignores the data range: $ n (1 \ le n \ le 100), m (1 \ le m \ le 20) $
You can see $ n $ and $ m $ is very small, so why do not we do it direct violence.
But this violence is also based on the basis of $ dp $ on.
First, you can set up a simple state:
$ Dp [i] $: minimum cost of $ I $ day to be used
Then turn and call out like
$dp[i]=dp[j]+cost[j+1][i]*(i-j)+K$
Which, $ j $ is we change the course of the day. This shaped turn means that from the $ j $ day began to replace the new channel, the first $ j + 1 $ day to the first $ i $ days using the same channel, the first $ j $-day how to go no matter, because this is things in front of treatment does not affect our stage.
So now only a question: $ cost [i] [j] $ how demand it?
Starting from the definition:
$ Cost [i] [j] $: from $ $ I $ J $ to day with the same minimum cost day waterway.
Go back to the previously mentioned, this question is very small data range, then we can deal with the direct violence $ cost [i] [j] $
Then the code will come out
#include<bits/stdc++.h> using namespace std; #define int long long template<class T>inline void qread(T& x){ char c;bool f=false;x=0; while((c=getchar())<'0'||'9'<c)if(c=='-')f=true; for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48)); if(f)x=-x; } template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);} inline int rqread(){ char c;bool f=false;int x=0; while((c=getchar())<'0'||'9'<c)if(c=='-')f=true; for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48)); return f?-x:x; } template<class T>inline T Max(const T x,const T y){return x>y?x:y;} template<class T>inline T Min(const T x,const T y){return x<y?x:y;} template<class T>inline T fab(const T x){return x>0?x:-x;} const int MAXN=100; const int MAXM=20; class gragh{ #define EDGE_SIZE 400 #define NODE_SIZE 20 #define INF 0x3f3f3f3f public: int N,M; struct edge{int to,nxt,w; edge(){} edge(const int T,const int Nx,const int W):to(T),nxt(Nx),w(W){} }e[EDGE_SIZE+5]; int tail[NODE_SIZE+5],ind; inline void add_edge(const int u,const int to,const int w){e[++ind]=edge(to,tail[u],w);tail[u]=ind;} inline void dijkstra(int dis[],const int s,bool vis[]){ struct node{int u,w; node(){} node(const int U,const int W):u(U),w(W){} bool operator<(const node a)const{return !(w<a.w);} }; for(int i=1;i<=N;++i)dis[i]=INF; priority_queue<node>Q; Q.push(node(s,dis[s]=0)); while(!Q.empty()){ int u=Q.top().u;Q.pop(); for(int i=tail[u],v;i;i=e[i].nxt)if(dis[v=e[i].to]>dis[u]+e[i].w&&!vis[v]) Q.push(node(v,(dis[v]=dis[u]+e[i].w))); } } inline bool spfa(int dis[],const int s,bool vis[]){ int cnt[NODE_SIZE+5];/*bool vis[EDGE_SIZE+5];*/ queue<int>Q; for(int i=1;i<=N;++i)dis[i]=INF/*,vis[i]=false*/,cnt[i]=0; Q.push(s); dis[s]=0,cnt[s]=1; while(!Q.empty()){ int now=Q.front();Q.pop(); vis[now]=false,++cnt[now]; if(cnt[now]>N)return false; for(int i=tail[now],v;i;i=e[i].nxt){ v=e[i].to; if(dis[now]+e[i].w<dis[v]){ dis[v]=dis[now]+e[i].w; if(!vis[v])Q.push(v),vis[v]=true; } } } return true; } inline void clr(){memset(tail,ind=0,sizeof tail);} gragh(){clr();} #undef EDGE_SIZE #undef NODE_SIZE #undef INF }G; int n,K,d,cost[MAXN+5][MAXN+5]; bool inf[MAXM+5][MAXN+5]; inline void init(){ qread(n,G.N,K,G.M); for(int i=1,a,b,c;i<=G.M;++i){ qread(a,b,c); G.add_edge(a,b,c); G.add_edge(b,a,c); } qread(d); for(int i=1,P,a,b;i<=d;++i){ qread(P,a,b); for(int j=a;j<=b;++j)inf[P][j]=true; } } void calcCost(){ bool vis[MAXM+5];int dis[MAXM+5]; for(int i=1;i<=n;++i)for(int j=i;j<=n;++j){ memset(vis,0,sizeof vis); for(int k=1;k<=G.N;++k)for(int t=i;t<=j;++t) if(inf[k][t]){vis[k]=true;break;} G.spfa(dis,1,vis); cost[i][j]=dis[G.N]; } } inline void getDp(){ int dp[MAXN+5]; for(int i=1;i<=n;++i)dp[i]=0x3f3f3f3f; for(int i=1;i<=n;++i){ dp[i]=cost[1][i]*i; for(int j=i-1;j>=0;--j) dp[i]=Min(dp[i],dp[j]+cost[j+1][i]*(i-j)+K); } printf("%lld\n",dp[n]); } signed main(){ init(); calcCost(); getDp(); return 0; }