HDU 4522 Shortest Path

Source: Internet
Author: User
Tags acos

Topic Description: Give the train path, and whether each path is a hard seat or a soft sleeper, and give the discomfort of the hard seat and the soft sleeper, and find the minimum discomfort from the start point to the end point.

Idea: First, find the distance from the start point to the end point of the hard seat and the soft sleeper respectively, and the last step is not comfortable. Of course, we need to create two diagrams, one is the hard seat path and the other is the soft sleeper path, note that when K = 1, there are both hard-seat soft sleepers, so both sides must be added.

Nothing else is the most basic short circuit. I didn't take a good look at question = during the competition. =


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 2005
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
# Define FOR (I, s, t) for (int I = (s); I <= (t); ++ I)
# Define ll long
# Define mem (a, B) memset (a, B, sizeof ())
# Define mp (a, B) make_pair (a, B)
Using namespace std;
 
Int head [2] [500];
Struct kdq
{
Int s, e, next;
} Edge [2] [2000];
Int num [2];
Bool vis [2] [500];
Void add (int s, int e, int k)
{
Edge [k] [num [k]. e = e;
Edge [k] [num [k]. next = head [k] [s];
Head [k] [s] = num [k] ++;
}
Void init ()
{
Mem (head,-1 );
Mem (vis, 0 );
Num [0] = num [1] = 0;
}
Char a [10005];
Int StringToInt (string x)
{
Int l = x. size ();
Int num = 0;
For (int I = l-1; I> = 0; I --)
{
Num + = (x [I]-'0') * pow (10.0, (double) (l-I-1 ));
}
Return num;
}
Int dis [2] [500];
Int n;
# Define x first
# Define y second
Int spfa (int s, int e, int k)
{
For (int I = 0; I <= n; I ++) dis [k] [I] = inf;
Dis [k] [s] = 0;
Vis [k] [s] = 1;
Queue <pair <int, int> q;
Q. push (mp (s, 0 ));
While (! Q. empty ())
{
Int temp = q. front (). x;
Int step = q. front (). y;
Q. pop ();
Vis [k] [temp] = 0;
If (temp = e)
Return step;
For (int I = head [k] [temp]; I! =-1; I = edge [k] [I]. next)
{
Int tt = edge [k] [I]. e;
If (dis [k] [tt]> dis [k] [temp] + 1)
{
Dis [k] [tt] = dis [k] [temp] + 1;
If (! Vis [k] [tt])
{
Vis [k] [tt] = 1;
Q. push (mp (tt, step + 1 ));
}
}
}
}
Return-1;
}
Int main ()
{
Int T;
Cin> T;
While (T --)
{
Int m;
Cin> n> m;
Init ();
While (m --)
{
Scanf ("% s", );
Int k;
Cin> k;
Int l = strlen ();
String x;
X. clear ();
Vector <int> q;
For (int I = 0; I <l; I ++)
{
If (a [I] = '+ ')
{
Q. push_back (StringToInt (x ));
X. clear ();
}
Else
X + = a [I];
}
Q. push_back (StringToInt (x ));
L = q. size ();
For (int j = 0; j <= k; j ++) // when k = 1, add the hard seat and soft sleeper to this path.
For (int I = 1; I <l; I ++)
{
Add (q [I-1], q [I], j );
}
// For (int I = 0; I <l; I ++) cout <q [I] <endl;
Q. clear ();
}
Int s, e, S, D;
Cin> S> D> s> e;
Int step1 = spfa (s, e, 0); // distance of the hard seat
Int step2 = spfa (s, e, 1); // distance of the soft sleeper
// Cout <step1 * S <"" <step2 * D <endl;
If (step1 =-1 & step2 =-1) // unable to arrive
Cout <-1 <endl;
Else
{
If (step1 =-1)
Cout <step2 * D <endl;
Else if (step2 =-1)
Cout <step1 * S <endl;
Else
Cout <min (step1 * S, step2 * D) <endl;
}
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 2005
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
# Define FOR (I, s, t) for (int I = (s); I <= (t); ++ I)
# Define ll long
# Define mem (a, B) memset (a, B, sizeof ())
# Define mp (a, B) make_pair (a, B)
Using namespace std;

Int head [2] [500];
Struct kdq
{
Int s, e, next;
} Edge [2] [2000];
Int num [2];
Bool vis [2] [500];
Void add (int s, int e, int k)
{
Edge [k] [num [k]. e = e;
Edge [k] [num [k]. next = head [k] [s];
Head [k] [s] = num [k] ++;
}
Void init ()
{
Mem (head,-1 );
Mem (vis, 0 );
Num [0] = num [1] = 0;
}
Char a [10005];
Int StringToInt (string x)
{
Int l = x. size ();
Int num = 0;
For (int I = l-1; I> = 0; I --)
{
Num + = (x [I]-'0') * pow (10.0, (double) (l-I-1 ));
}
Return num;
}
Int dis [2] [500];
Int n;
# Define x first
# Define y second
Int spfa (int s, int e, int k)
{
For (int I = 0; I <= n; I ++) dis [k] [I] = inf;
Dis [k] [s] = 0;
Vis [k] [s] = 1;
Queue <pair <int, int> q;
Q. push (mp (s, 0 ));
While (! Q. empty ())
{
Int temp = q. front (). x;
Int step = q. front (). y;
Q. pop ();
Vis [k] [temp] = 0;
If (temp = e)
Return step;
For (int I = head [k] [temp]; I! =-1; I = edge [k] [I]. next)
{
Int tt = edge [k] [I]. e;
If (dis [k] [tt]> dis [k] [temp] + 1)
{
Dis [k] [tt] = dis [k] [temp] + 1;
If (! Vis [k] [tt])
{
Vis [k] [tt] = 1;
Q. push (mp (tt, step + 1 ));
}
}
}
}
Return-1;
}
Int main ()
{
Int T;
Cin> T;
While (T --)
{
Int m;
Cin> n> m;
Init ();
While (m --)
{
Scanf ("% s", );
Int k;
Cin> k;
Int l = strlen ();
String x;
X. clear ();
Vector <int> q;
For (int I = 0; I <l; I ++)
{
If (a [I] = '+ ')
{
Q. push_back (StringToInt (x ));
X. clear ();
}
Else
X + = a [I];
}
Q. push_back (StringToInt (x ));
L = q. size ();
For (int j = 0; j <= k; j ++) // when k = 1, add the hard seat and soft sleeper to this path.
For (int I = 1; I <l; I ++)
{
Add (q [I-1], q [I], j );
}
// For (int I = 0; I <l; I ++) cout <q [I] <endl;
Q. clear ();
}
Int s, e, S, D;
Cin> S> D> s> e;
Int step1 = spfa (s, e, 0); // distance of the hard seat
Int step2 = spfa (s, e, 1); // distance of the soft sleeper
// Cout <step1 * S <"" <step2 * D <endl;
If (step1 =-1 & step2 =-1) // unable to arrive
Cout <-1 <endl;
Else
{
If (step1 =-1)
Cout <step2 * D <endl;
Else if (step2 =-1)
Cout <step1 * S <endl;
Else
Cout <min (step1 * S, step2 * D) <endl;
}
}
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.