590 - Always on the run
Posted: Mon Jul 21, 2003 10:29 am
Can the problem-590 be solved by BFS?
If it can then how.
plz explain.
If it can then how.
plz explain.
Code: Select all
for i = 1 to n // n total places
for d = 1 to day // day total days
val[i][d] = inf
set val[1][0] = 0, because in 0-th day she is in the 1st place. And the cost is zero.
1. enque(1, 0)
2. u = deque()
3. for i = 1 to n and i is different from u.n
4. v.n = i and v.day = u.day+1
5. if(there is a flight in the u.day-th day from u.n to v.n)
6. if(val[v.n][v.day] > val[u.n][u.day] + flight cost)
7. update val[v.n][v.day] and enque(v)
8. if the queue is not empty then goto step 2
then the val[n][day] will contain the minimum cost.
Code: Select all
int n,k;
vector<int> D[11][11]; //D[city1][city2][day]
long long dp[11][31]; //dp[city][day]
long long solve(int city,int day)
{
if(day==k){
if(city==n) return 0;
return INF;
}
long long& res=dp[city][day];
if(res!=-1) return res;
res=INF;
for(int i=1;i<=n;i++)
{
if(city==i) continue;
int sz=D[city][i].size();
if(D[city][i][day%sz]!=0) res=min(res,D[city][i][day%sz]+solve(i,day+1));
}
return res;
}
day could be as large as 1000 ..so you should increase size of 2nd dimension of your array..I was also getting WA for this reason..long long dp[11][31]; //dp[city][day]
Code: Select all
AC
Code: Select all
#include <bits/stdc++.h>
using namespace std;
/*------- Constants---- */
#define LMT 12
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define MEMSET_INF 63
#define MEM_VAL 1061109567
#define FOR(i,n) for( int i=0 ; i < n ; i++ )
#define mp(i,j) make_pair(i,j)
#define lop(i,a,b) for( int i = (a) ; i < (b) ; i++)
#define pb(a) push_back((a))
#define gc getchar_unlocked
#define PI acos(-1.0)
#define inf 1<<28
#define lc ((n)<<1)
#define rc ((n)<<1 |1)
#define msg(x) cout<<x<<endl;
#define EPS 1e-7
/*---- short Cuts ------- */
#define ms(ara_name,value) memset(ara_name,value,sizeof(ara_name))
typedef pair<int, int> ii;
typedef vector<int > vi ;
/*------ template functions ------ */
inline void sc(int &x)
{
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 | c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
template <class T> inline T bigmod(T p,T e,T M){
ll ret = 1;
for(; e > 0; e >>= 1){
if(e & 1) ret = (ret * p) % M;
p = (p * p) % M;
} return (T)ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}
/*************************** END OF TEMPLATE ****************************/
int N , K ;
int cost[LMT][LMT][1005];
bool flag = false;
int dp[LMT][1005];
int var[LMT];
int cal(int n,int day)
{
if( day >= K ){
if( n == N ) {
flag = true;
return 0;
}
else return inf ;
}
if(dp[n][day] !=-1 ) return dp[n][day];
int t = inf ;
for(int i = 1; i <= N ; i ++ ) {
if(i != n ) {
if(cost[n][i][day % var[n]] !=0 ) {
t = min (t , cost[n][i][day % var[n] ] + cal(i, day + 1) );
}
}
}
return dp[n][day] = t;
}
int main()
{
int cs = 0 , p ;
while (1) {
sc(N);sc(K);
if(N == 0 && K == 0 ) break;
ms(cost, 0);
for(int i = 1 ; i <= N ; i ++ ) {
for(int j = 1; j <= N ; j ++ ) {
if( i == j ) continue;
sc(p);
var[i] = p;
for(int l = 0 ; l < p ; l ++ ) {
sc(cost[i][j][l]);
}
}
}
ms(dp, -1);
flag = false;
int p = cal(1 , 0);
printf("Scenario #%d\n",++cs);
if(flag ){
printf("The best flight costs %d.\n",p);
}
else printf("No flight possible.\n");
printf("\n");
}
return 0;
}