Nyoj 1208--Water Problem series —————— "DP"

Source: Internet
Author: User

Water Problem Series time limit:MS | Memory limit:65535 KB Difficulty:2
Describe
give you a map, each side has a certain weight, and now let you start at any point, the weight of the side of each walk must be the weight of the last time a large number of cases, ask you can go a few sides?
Input
first an n and M, respectively, representing the number of points and the number of edges
The next M-line, three values per line X,y,val, indicates that X-to-Y has a path with a weighted value of Val. (1<n,m,val<10^5,0<x,y<=n)
Output
the maximum number of edges to output
Sample input
3 31 2 12 3 13 1 16 71 2 13 2 52 4 22 5 22 6 95 4 34 3 4
Sample output
 



Problem Solving: The topic just seems to be a graph theory, but the subject is the DP thought of the investigation. Defines two arrays Dp[i],g[i] indicates the maximum number of forward edges at which the edge is numbered I, and when the point is I. The transfer equation is:
If the edge rights are different: Dp[i]=g[e[i].st]+1,g[e[i].en]=max (G[e[i].en],dp[i]).
If the edge rights are the same: {i,i+1,i+2,i+3 ...} Edges in the collection are numbered with the same edge.
Dp[i]=g[e[i].st]+1,dp[i+1]=g[e[i+1].st]+1,etc ...
G[e[i].en]=dp[i],g[e[i+1].en]=dp[i+1],etc ...
for the same side of the same situation, the first set of examples can see the problem, here the operation of the seniors said is the delay, specifically should be the point of delay, on the edge ahead.


#include <bits/stdc++.h>using namespace Std;const int maxe=1e5+10;struct edge{int st,en; int Val;} E[maxe];int dp[maxe],g[maxe];int inf=1e9;bool cmp (Edge A,edge b) {return a.val<b.val;}    void solve (int n,int m) {memset (dp,0,sizeof (DP));    memset (G,0,sizeof (g));            for (int i=0;i<m;i++) {if (e[i].val<e[i+1].val) {dp[i]=g[e[i].st]+1;        G[e[i].en]=max (G[e[i].en],dp[i]);            }else{int j,t=i;            for (j=i;j<m;j++) {if (e[j].val!=e[j+1].val) break;            } i=j;            for (int k=t;k<=j;k++) {dp[k]=g[e[k].st]+1;            } for (int k=t;k<=j;k++) {G[e[k].en]=max (g[e[k].en],dp[k]); }}}} int max_e=0;//for (int i=1;i<=n;i++) {//Traversal point and edge should be the same effect, but the test data in the background is ... if (g[i]>max_e) {//max_e=g[i];//}//} for (int i=0;i<m;i++) {if (max_ E<dp[i]) {                Max_e=dp[i]; }} printf ("%d\n", max_e);}    int main () {freopen ("In.txt", "R", stdin);    Freopen ("OUT1.txt", "w", stdout);    int n,m; while (scanf ("%d%d", &n,&m)!=eof) {for (int i=0;i<m;i++) {scanf ("%d%d%d", &e[i].st,&e[i].        En,&e[i].val);        } sort (e,e+m,cmp);        E[m].val=inf;    Solve (n,m); } return 0;}

  

Nyoj 1208--Water Problem series —————— "DP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.