1113 Tournament Summary

1 catch the ball

Face problems not say, that is a normal probability DP, but I did not find time to see a huge range of range xi, yi, violent determination of probability, the results of the burst. So the lesson is, one lesson is not necessarily the data range from small to large range, and the second is to not just look at the range of N, M and the like.

Communication block count X
Time limit: - MS    Space limitations: - KB 
Benchmark description: 1s, 128m
Problem Description

Gives a tree of n points, each point has a weight ai. Selecting a point from this tree set point such that the selected communication, and satisfies the weight difference between the maximum and minimum points of the point set not more than k, asked how many species selected from the set point approach.
Two options are different approaches set point if and only if point set different reference points.

Input Format

The first line, contains two integers n, k.
The second line contains n integers a1, a2, · · ·, an.
Next, n - 1 rows, each row comprising two positive integers u, v, represents u, v there is an edge between two points.

Output Format

Only the output line, comprising a number representing the selected set point of approach.
This number can be large, the output of the modulo 1000000007.

Sample input 1

4 1
2 1 3 2
1 2
1 3
3 4

Sample output 1

8

Since this question a little right, let me think of DP, but if the state set as the difference, it is difficult to transfer, and then jammed.

Since this scheme difficult problem is to determine the position of the maximum and minimum points, so we set f [i] for the i in the subtree rooted Number

Difficult to think transfer equation: F [I] = [pi] (F [J] + 1'd) 

Another problem is to limit the difference, I began to want it set into the state, but can not be transferred

 

Note, however, say the topic is "communication block", which means that the root is uncertain .

We enumerate each point x as long as the root, with its maximum value, so long as any v [x] -k <= v [y] <= v [x] will be able to

However, when the same two adjacent points weights, they may be found to each other.

This time we artificially set a priority, only one point less than the number found its point, the problem is solved

Looking back, in addition to traversing a tree, this question did not use any high-level algorithm, but contains the idea "of moving to static"

Since I can not determine which one is the point of maximum point, we will make it a point each for the maximum point

Since there may be double counting, we formulate restrictions and rules to calculate

 

#include<bits/stdc++.h>
#define ll long long using namespace std; struct node{ ll to;ll next; }e[4010]; ll last[2010],tot; void add(ll x,ll y){ e[++tot].to=y; e[tot].next=last[x]; last[x]=tot; } ll n,k,a[2010],f[2010],mod=998244353; ll dfs(ll x,ll fa,ll root){ ll tot=1; for(ll i=last[x];i;i=e[i].next){ ll y=e[i].to; if(y!=fa&&a[root]>=a[y]&&a[root]-a[y]<=k&&(root>y||a[root]!=a[y])){ tot=(tot*(dfs(y,x,root)+1))%mod; } } return tot; } int main() { ll i,j,x,y; scanf("%lld%lld",&n,&k); for(i=1;i<=n;i++)scanf("%lld",&a[i]); for(i=1;i<=n-1;i++){ scanf("%lld%lld",&x,&y); add(x,y);add(y,x); } ll ans=0; for(i=1;i<=n;i++){ ans+=dfs(i,i,i);ans%=mod; //cout<<dfs(i,i,i)<<endl; } cout<<ans<<endl; return 0; }
W hero new technologies
Time limit: - MS    Space limitations: 165536 KB 
Benchmark description: 1000ms
Problem Description

Zhang also prepared to indulge in lol escape. To show his sincerity, Zhang designed a new hero. The big move new hero is very strong, in the chase when people can reflect a very strong advantage. Summoner Canyon is assumed that there are n nodes a, m unidirectional edges FIG. For each node x, x is at all possible to the end of reducing the weight of the side d (-10000 <= d <= 10000), while the all sides starting from the x weights plus d. Make minimum maximum weights of all edges. Of course, the right side of the value can not be negative, because it does not comply with the laws of physics Summoner Canyon.

Input Format

Multiple sets of data, for each set of data:

The first two acts of integers n, m

Subsequently m rows, each with three integers a, b, c there from a to b represents a length of the road c

Output Format

For each data block output file only one row:

If the answer is one and only one solution, maximum possible value of the output of the shortest road

If the answer is arbitrary, that is how the solution, output "Infinite"

If no solution, output "No Solution"

Sample input

2 1
1 2 10
2 1
1 2 -10
3 3
1 2 4
2 3 2
3 1 5
4 5
2 3 4
4 2 5
3 4 2
3 1 0
1 2 -1

Sample Output

Infinite
Infinite
3
1

prompt

n≤500, m≤2700, -10000 <= d <= length of each road 10,000 to ensure that no more than 10,000

Each point is the meaning of the questions about all the sides and the edges can be zero and any changes in the maximum value for the Minimum edge

Although this question undirected graph background, but quite different from the general graph theory

See minimum maximum, naturally think half the answer. How to determine half later, the time difference constraint on stage! !

Generally, we use the differential restraint systems are seeking the maximum or minimum value, to determine whether the solvability of some rare, but can not be ignored.

Maybe next time will be a judgment whether there is any solution.

This question is to determine two special cases to get me explode

In fact, half of it for good judgment, as long as half of the lower bound is still not satisfied with the conditions, that is no solution; in the community or +1 position to meet the conditions, there is any solution

 

#include<bits/stdc++.h>
using namespace std; inline int read() { int x=0,k=1;char ch; ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')k=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*k; } struct node{ int to;int next;int len; }e[6010]; int last[510],tot; void add(int x,int y,int z){ e[++tot].to=y; e[tot].len=z; e[tot].next=last[x]; last[x]=tot; } int n,m,lx[6010],ly[6010],wei[6010]; int dis[510],book[510],cnt[510],ss; queue<int>q; void spfa(){ memset(dis,0x3f,sizeof(dis));memset(book,0,sizeof(book));memset(cnt,0,sizeof(cnt)); while(q.size())q.pop(); dis[0]=0;q.push(0);book[0]=1; while(q.size()){ int t=q.front();q.pop();book[t]=0; cnt[t]++; for(int i=last[t];i;i=e[i].next){ int s=e[i].to;int l=e[i].len; if(dis[t]+l<dis[s]){ dis[s]=dis[t]+l; if(book[s]==0){ q.push(s);book[s]=1; if(cnt[s]>n+1){ ss=0;return; } } } } } } int judge(int mid){ int i,j; memset(e,0,sizeof(e));memset(last,0,sizeof(last));tot=0; for(i=1;i<=m;i++){ add(lx[i],ly[i],wei[i]-mid); } for(i=1;i<=n;i++)add(0,i,0); ss=1;spfa(); //cout<<ss<<endl; if(ss==0)return 0; int maxn=-1,minn=0x3f3f3f3f; for(i=1;i<=n;i++){ maxn=max(maxn,dis[i]); minn=min(minn,dis[i]); } //cout<<maxn<<" "<<minn<<endl; //if(maxn-minn<=20000)return 1; return 1; } int ef(int l,int r){ int mid; while(l<r){ mid=(l+r+1)>>1; if(judge(mid)==1)l=mid; else r=mid-1; } return l; } int main() { //freopen("newhero.in","r",stdin); //freopen("oops.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF) { int i,j,k,a,b,c; memset(lx,0,sizeof(lx));memset(ly,0,sizeof(ly));memset(wei,0,sizeof(wei)); for(i=1;i<=m;i++){ scanf("%d%d%d",&a,&b,&c);lx[i]=a;ly[i]=b;wei[i]=c; } //cout<<judge(2418)<<endl; //for(i=1;i<=n;i++)cout<<dis[i]<<" "; if(judge(1)==0){ cout<<"No Solution"<<endl;continue; } if(judge(10001)==1){ cout<<"Infinite"<<endl;continue; } cout<<ef(1,10000)<<endl; } return 0; } 

Guess you like

Origin www.cnblogs.com/pepparan/p/11854586.html