HDU4396 More lumber is required

Source: Internet
Author: User

2012 Multi-University Training Contest 10
1007 questions
Two-dimensional shortest path.

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Const int MAXN = 5005;
Const int MAXM = 200005;
Const int MAXK = 505;
Const int MAXQ = MAXN * MAXK;
Constint INF = 1000000000;
 
Struct Edge
{
Int v, w, next;
} Edge [MAXM];
Int edgeNumber, head [MAXN];
Int n, m;
Int s, t, k;
Int ans;
 
Inline void clearEdge ()
{
EdgeNumber = 0;
Memset (head,-1, sizeof (head ));
}
 
Inline void addEdge (int u, int v, int w)
{
Edge [edgeNumber]. v = v;
Edge [edgeNumber]. w = w;
Edge [edgeNumber]. next = head [u];
Head [u] = edgeNumber ++;
}
 
Int queue [MAXQ] [2];
Bool visit [MAXN] [MAXK];
Int dist [MAXN] [MAXK];
 
Void solve ()
{
Ans =-1;
Int front = 0, rear = 1;
For (int I = 1; I <= n; ++ I)
{
For (int j = 0; j <= k; ++ j)
{
Visit [I] [j] = false;
Dist [I] [j] = INF;
}
}
Queue [0] [0] = s;
Queue [0] [1] = 0;
Visit [s] [0] = true;
Dist [s] [0] = 0;
While (front! = Rear)
{
Int u = queue [front] [0];
Int uk = queue [front] [1];
For (int I = head [u]; I! =-1; I = edge [I]. next)
{
Int v = edge [I]. v;
Int vk = uk + 10;
Int w = edge [I]. w;
If (vk> k)
{
Vk = k;
}
If (dist [v] [vk]> dist [u] [uk] + w)
{
Dist [v] [vk] = dist [u] [uk] + w;
If (v = t & vk = k)
{
Ans = dist [v] [vk];
}
If (! Visit [v] [vk])
{
Visit [v] [vk] = true;
Queue [rear] [0] = v;
Queue [rear] [1] = vk;
If (++ rear> = MAXQ)
{
Rear = 0;
}
}
}
}
Visit [u] [uk] = false;
If (++ front> = MAXQ)
{
Front = 0;
}
}
}
 
Int main ()
{
Int u, v, w;
While (~ Scanf ("% d", & n, & m ))
{
ClearEdge ();
For (int I = 0; I <m; ++ I)
{
Scanf ("% d", & u, & v, & w );
AddEdge (u, v, w );
AddEdge (v, u, w );
}
Scanf ("% d", & s, & t, & k );
Solve ();
Printf ("% d \ n", ans );
}
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.