您的位置:首页 > 其它

UVA 11280 - Flying to Fredericton

2013-03-19 17:54 246 查看
/*

最短路问题,

题意:给出一个q 代表 到达目的地的过程中最多可以停几站。

方法: 用一个dp数组 dp[i][j] 代表第i站的时候停站j次的最小花费。

然后bfs就可以

*/

#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<=len&&dp[v][s+1]>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",&sp);

//printf("%d..!!!!\n",sp);

bfs(strat,end,sp+1);

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: