UVA 11280-flying to Fredericton

Source: Internet
Author: User
Tags printf strcmp

/*

The shortest circuit problem,

Test instructions: A Q represents the maximum number of stops that can be stopped in the process of arriving at a destination.

Method: Use a DP array dp[i][j] to represent the minimum cost of Stop J times for station I.

Then BFS will be able to

*/


#include <cstdio> #include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
#define M 1010
using namespace Std;


struct Edge
{
int from,to,dist;
};
struct Node
{
int u,cost,step;
};
Char ct[110][30];
Vector<edge> edges;
Vector<int> G[m];
Map<string,int> ID;
int n,m,inq[m][m],cnt[m],dp[m][m];
int init ()
{
Id.clear ();
Edges.clear ();
for (int i = 0; i < n; i++)
G[i].clear ();
}
int add (int from,int to,int Dist)
{
Edges.push_back (Edge)
{
From,to,dist
});
int L = edges.size ();
G[from].push_back (L-1);
}
int BFS (int strat,int end,int len)
{
Queue<node> Q;
memset (inq,0,sizeof (INQ));
int INF = 1<<30;
for (int i = 0; i < n; i++)
for (int j = 0; J <= Len; j + +)
DP[I][J] = INF;
while (!q.empty ())
Q.pop ();
Dp[strat][0] = 0;
Inq[strat][0] = 1;
Node A;
A.U = Strat;
A.cost = 0;
A.step = 0;
Q.push (a);
int u,v,s,c,dd;
while (!q.empty ())
{
A = Q.front ();
Q.pop ();
u = a.u;
c = a.cost;
s = a.step;
printf ("-----\n%d%d%d\n", u,s,c);
Inq[u][s] = 0;
for (int i = 0; i < g[u].size (); i++)
{
Edge &e = edges[g[u][i]];
v = e.to;
dd = e.dist;
if (S+1&LT;=LEN&AMP;&AMP;DP[V][S+1]&GT;DP[U][S]+DD)
{
DP[V][S+1]=DP[U][S]+DD;
if (!inq[v][s+1])
{//printf ("%d%d\n", v,s+1);
INQ[V][S+1] = 1;
a.u = v;
A.step = s+1;
A.cost = dp[v][s+1];
Q.push (a);
}
}
}
}
int ans = INF;
for (int i = 0; I <= len; i++)
if (ans > dp[end][i])
ans = dp[end][i];
if (ans = = INF)
printf ("No satisfactory flights\n");
Else
printf ("Total cost of flight (s) is $%d\n", ans);
}
int main ()
{
int t,strat,end,count=0;
scanf ("%d", &t);
while (t--)
{
count++;
scanf ("%d", &n);
Init ();
for (int i = 0; i < n; i++)
{
scanf ("%s", Ct[i]);
Id[ct[i]] = i;
if (!strcmp (Ct[i], "Calgary"))
Strat = i;
if (!strcmp (Ct[i], "Fredericton"))
end = i;
}
scanf ("%d", &m);
Char t1[30],t2[30];
int d,u,v;
for (int i = 0; i < m; i++)
{
scanf ("%s%s%d", t1,t2,&d);
U = id[t1];
v = id[t2];
Add (u,v,d);
}
if (count>1)
Puts ("");
printf ("Scenario #%d\n", count);
int p,sp;
scanf ("%d", &p);
while (p--)
{
scanf ("%d", &AMP;SP);
printf ("%d..!!!! \ n ", SP);
BFS (strat,end,sp+1);
}
}
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.