hdu3416 determine if the shortest path is unique (only one time per side)

Source: Internet
Author: User

Marriage Match IVTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3147 Accepted Submission (s): 946


Problem Descriptiondo not sincere non-interference.
Like the show, now Starvae also take part in a show, but it take place between City A and B. Starvae are in city A and GIR LS is in city B. Every time starvae can get to City B and make a data with a girl he likes. But there was problems with it, one was starvae must get to B within least time, it's said that he must take a shortest Path. Other are no road can be taken more than once. While the city starvae passed away can been taken more than once.


So, under a good RP, starvae May has many chances to get to City B. But he's don ' t know how many chances at most he can make a data with the girl he likes. Could help starvae?
Inputthe first line is a integer T indicating the case number. (1<=t<=65)
For each case,there is both integer n and m in the first line (2<=n<=1000, 0<=m<=100000), n is the number of The city and M are the number of the roads.

Then follows M line, each line has three integers a,b,c, (1<=a,b<=n,0<c<=1000) It means there is a road from a to B and it's distance are C, while there may has no road from B to a. There May has a road from a to a,but you can ignore it. If There is roads from a to B, they is different.

At last was a line with both integer A and B (1<=a,b<=n,a!=b), means the number of city A and city B.
There may is some blank line between the case.
Outputoutput a line with a integer, means the chances starvae can get on most.
Sample Input
3 7 8 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 5 7 1 6 7 1 1 7 6 7 1 2 1 2 3 1 1 3 3 3 4 1 3 5 1 4 6 1 5 2 1 2 1 1 2 2 1 2
Sample Output2 1 1 Test instructions: There are n cities, M-Bar, A to B is consumed as C, is a one-way edge. The shortest path from S to T is required, and each side can only walk once. Idea: If you don't have to walk once a day, you can solve it by using a tree-shaped DP. But here each side can only walk once, that is, each path above the edge of the traffic is 1, from the starting point to the end of the maximum flow can be. So the problem can be solved with the maximum flow. For a new diagram, you can first find the shortest path from the endpoint to the starting point, and then start Dfs from the starting point, and maintain the now[] array, indicating the length of the route from the starting point to the point, if NOW[RT] + dis[t] + Edge[i].val = = Dis[s] (dis[) The starting point is the original figure in the end point), then the two points are the shortest way points, then you can connect the edge, the flow is 1. Run a maximum flow to solve the problem.

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<string>#include<time.h>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineINF 1000000001#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMAXN =1010;structnode{intto ; intVal; intNext;} EDGE[MAXN* -*2],e[maxn* $*5];intInd,pre[maxn],vis[maxn],dis[maxn],pre1[maxn],ind1;intnow[maxn],s,t;intn,m;voidADD1 (intXintYintz) {e[ind1].to=y; E[ind1].val=Z; E[ind1].next=Pre1[x]; PRE1[X]= Ind1 + +;}voidSPFA () { for(inti =1; I <= N; i++) {Dis[i]=INF; Vis[i]=0; } Vis[t]=1; Dis[t]=0; Queue<int>P;    Q.push (T);  while(!Q.empty ()) {        intTP =Q.front ();        Q.pop (); VIS[TP]=0;  for(inti = PRE1[TP]; I! =-1; i =E[i].next) {            intt =e[i].to; if(Dis[t] > DIS[TP] +e[i].val) {Dis[t]= DIS[TP] +E[i].val; if(!Vis[t]) {Vis[t]=1;                Q.push (t); }            }        }    }}voidAddintXintYintz) {edge[ind].to=y; Edge[ind].val=Z; Edge[ind].next=Pre[x]; PRE[X]= ind + +;}voidDFS1 (intRT) {Vis[rt]=1; if(RT = = T)return ;  for(inti = Pre1[rt]; I! =-1; i =E[i].next) {        intt =e[i].to; if(Now[rt] + dis[t] + E[i].val = =Dis[s]) {Now[t]= Now[rt] +E[i].val; Add (Rt,t,1); Add (T,rt,0); if(!Vis[t])            {DFS1 (t); }        }    }}intBFs () {memset (Vis,-1,sizeof(VIS)); Queue<int>P; Vis[s]=0;    Q.push (S);  while(!Q.empty ()) {        intTP =Q.front ();        Q.pop ();  for(inti = PRE[TP]; I! =-1; i =Edge[i].next) {            intt =edge[i].to; if(Vis[t] = =-1&&edge[i].val) {Vis[t]= VIS[TP] +1;            Q.push (t); }        }    }    if(Vis[t] = =-1)return 0; return 1;}intDfsintRtintLow ) {    intused =0; if(RT = = T)returnLow ;  for(inti = Pre[rt]; I! =-1&& used < low; i =Edge[i].next) {        intt =edge[i].to; if(Vis[t] = = Vis[rt] +1&&edge[i].val) {            intA = DFS (T,min (low-used,edge[i].val)); Used+=A; Edge[i].val-=A; Edge[i^1].val + =A; }    }    if(used = =0) Vis[rt] =-1; returnused;}intx[maxn* -],y[maxn* -],z[maxn* -];voidInit (intflag) {Ind1=0; memset (Pre1,-1,sizeof(Pre1));  for(inti =1; I <= m; i++){        if(!flag)        {add1 (y[i],x[i],z[i]); }        Else{add1 (x[i],y[i],z[i]); }    }}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m);  for(inti =1; I <= m; i++) {scanf ("%d%d%d",&x[i],&y[i],&Z[i]); } Init (0); scanf ("%d%d",&s,&T);        SPFA (); Init (1); IND=0; Memset (now,0,sizeof(now)); memset (PRE,-1,sizeof(pre));        DFS1 (S); intAns =0;  while(BFS ()) { while(1){                intA =DFS (S,inf); if(!a) Break; Ans+=A; }} printf ("%d\n", ans); }    return 0;}

hdu3416 determine if the shortest path is unique (only one time per side)

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.