Garlic Garlic Spfa+dfs With a passion for work

Source: Internet
Author: User

As we all know, garlic garlic is a good staff who love to work, he felt that time is money, do things are always against the clock.

That night, garlic garlic a person to eat dinner. Unfortunately, after dinner, it began to rain, garlic and garlic did not bring out the umbrella. But garlic loves work, work makes him happy, he wants to hurry back to write code.

Garlic Company in Zhongguancun, Zhongguancun side of the terrain complex, there are many flyovers, underground channels and roads staggered together. Among them, the underground passage is able to take shelter from the rain, overpass and the road can not avoid. Zhongguancun can be abstracted into a nnn point map (vertex number 111 to nnn), of which there are m1m_1m 1 underground channels, m2m_2m 2 roads or flyovers, of which the length of the underground channel is 111. Garlic to eat place at 111 points, the company in NNN Point. Of course, garlic, although love to work, but he does not want to get a lot of rain, but also do not want to waste a lot of time. So he compromised--he wanted to get out of the rain and try as little as possible when the total distance he had taken to return to the company was less than equal to LLL.

Please hurry up and help the garlic garlic work to plan a path, don't let him waste time. Input Format

The first line enters the number of test groups T (1≤t≤20) T (1 \le t \le) t (1≤t≤20).

Next TTT the group data.

The first line of each set of data enters four integers n (2≤n≤100) n (2 \le n \le) n (2≤n≤100), M1 (0≤M1≤50) m_1 (0 \le m_1 \le) M 1 (0≤m 1≤50), M2 (0≤m2≤5000) m_2 ( 0 \le m_2 \le 5000) M 2 (0≤m 2≤5000), L (1≤l≤108) L (1 \le L \le 10^8) L (1≤l≤10 8).

Next to m1m_1m 1 lines, enter two integers per line a,b (1≤a,b≤n) A, B (1 \le A, b \le N) a,b (1≤a,b≤n), which means that there is an underground passage between AAA and BBB.

Next to m2m_2m 2 lines, enter three integers per line u,v (1≤u,v≤n), C (1≤c≤106) U, v (1 \le u, v \le N), C (1 \le C \le 10^6) u,v (1≤u,v≤n), C (1≤c≤10 6), indicating Between UUU and VVV there is a road or overpass with a length of CCC.

all paths are bidirectional . output Format

For each group of data, if there is a path to meet the requirements, output an integer, indicating the distance of the rain, otherwise output −1-1−1. Sample Input

3,
4 2 2 6 1 2 2 3 1 4 5 3 4 4 4 2 2 5 1 2 2 3 1 4 5 3 4 4 4 2 2 4 1 2
2 3
1 4 5 3 4 4
Sample Output
4
5
-1
topic Sources

Jing CHI Unmanned 1024 Programming Invitational

This title WA and Tlen Times, reference Daniel Blog

Solution: the shortest distance from N to every point is first obtained, and then the DFS is carried out on 1 points;

DFS: ① If the minimum precipitation from the current point of 1 points plus the rainfall from this to the next point is less than the amount of rain from the next point of 1, then the minimum amount of rain from the next point is updated; otherwise return

② if the distance from 1 to the current point is added to the distance from the next adjacent point, plus the shortest distance from the next point to n points is less than the limit distance, the DFS continues;

#include <iostream> #include <algorithm> #include <string.h> #include <queue> using Namespa  
CE std;
const int inf=0x3f3f3f3f;;
const int maxx=1e3+100;;
const int MAXN=2E6+10;
typedef long Long LL;  
int m1,m2,n,q,l;
int Dis[maxx],dis2[maxx];
int Sto[maxx];
int Pre[maxx];
int HEAD[2*MAXN]; 
BOOL Vis[maxx],vis2[maxx],vis3[maxx];
int Nu[maxx],flag;  
    struct point{int to; 
	int next;
	int Val;
int val2;  
}; 
Point PT[2*MAXN];  
          void Add (int u,int v,int val) {pt[q].next=head[u];
		  Pt[q].to=v;
		  Pt[q].val=val;
		  Pt[q].val2=val;
		  if (flag) pt[q].val2=0;
head[u]=q++;
	} void Spfa (int st) {queue<int>q;
    int w,v;
    memset (dis,inf,sizeof (dis));
    memset (Vis) (vis,false,sizeof);
    Q.push (ST);
    dis[st]=0;
    Vis[st]=true;
    	while (!q.empty ()) {V=q.front ();
    	Q.pop ();
    	Vis[v]=false;
    		for (int i=head[v];i!=-1;i=pt[i].next) {w=pt[i].val; if (w+dis[v]<dis[pt[I].to]) {DIS[PT[I].TO]=W+DIS[V];
    				if (!vis[pt[i].to]) {vis[pt[i].to]=true;
				 Q.push (pt[i].to);
	}}} void Dfs (int st,int number,int number2) {if (number<nu[st)) Nu[st]=number;
	else return;
	if (st==n) return; for (int i=head[st];i!=-1;i=pt[i].next) {if (number2+dis[pt[i].to]+pt[i].val<=l&&nu[st]+pt[i].val2<
		Nu[pt[i].to]) {DFS (pt[i].to,nu[st]+pt[i].val2,number2+pt[i].val);
    int main () {int t,t1,va,u,v,val;
    cin>>t;
		while (t--) {memset (nu,inf,sizeof (NU));
        scanf ("%d%d%d%d", &n,&m1,&m2,&l); 
        Q=1; 
        Memset (head) (head,-1,sizeof);
        flag=1;
        	for (int i=0;i<m1;i++) {scanf ("%d%d", &u,&v);
        	Add (u,v,1);
		Add (v,u,1);
		} flag=0;
			for (int i=0;i<m2;i++) {scanf ("%d%d%d", &u,&v,&val);
			Add (U,v,val);
		Add (V,u,val);
		} SPFA (n);
 if (dis[1]>l)       printf (" -1\n");
         else {DFS (1,0,0);
        printf ("%d\n", Nu[n]);  
} return 0;  }

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.