carter91620

_______________你知道的，搞ACM都是抽象男

poj3635---BFS

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1005
#define inf 1<<30
int sum[maxn],dis[maxn];
int cost[maxn],mincost[maxn][105];
bool vis[maxn][105];
int tot,n,m,t,st,en,qth,cap;
struct TNode
{
int to,next,cost,cap;
bool operator<(const TNode &temp) const
{
return cost>temp.cost;
}
};
vector<TNode>ans;
priority_queue<TNode>qu;
{
TNode tmp;
tmp.to=u,tmp.cost=w;
tmp.next=sum[v],sum[v]=tot++;
ans.push_back(tmp);
}
int bfs()
{
int k;
TNode tmp1,tmp2;
while(!qu.empty()) qu.pop();
memset(vis,0,sizeof(vis));
tmp1.to=st,tmp1.cap=0,tmp1.cost=0;
qu.push(tmp1);
while(!qu.empty())
{
tmp1=qu.top(),qu.pop();
if(tmp1.to==en) return tmp1.cost;
if(vis[tmp1.to][tmp1.cap]) continue;
vis[tmp1.to][tmp1.cap]=1;
tmp2.to=tmp1.to,tmp2.cap=tmp1.cap+1,tmp2.cost=tmp1.cost+cost[tmp1.to];
if(tmp2.cap<=cap&&!vis[tmp2.to][tmp2.cap]&&mincost[tmp2.to][tmp2.cap]>tmp2.cost)
{
mincost[tmp2.to][tmp2.cap]=tmp2.cost;
qu.push(tmp2);
}
for(k=sum[tmp1.to];k!=-1;k=ans[k].next)
{
if(tmp1.cap<ans[k].cost) continue;
tmp2.to=ans[k].to;
tmp2.cap=tmp1.cap-ans[k].cost;
tmp2.cost=tmp1.cost;
if(!vis[tmp2.to][tmp2.cap]&&mincost[tmp2.to][tmp2.cap]>tmp2.cost)
{
mincost[tmp2.to][tmp2.cap]=tmp2.cost;
qu.push(tmp2);
}
}
}
return -1;
}
int main()
{
int i,u,v,w,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=0;
memset(sum,-1,sizeof(sum));
for(i=0; i<n; i++) scanf("%d",&cost[i]);
ans.clear();
for(i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
}
scanf("%d",&qth);
while(qth--)
{
scanf("%d%d%d",&cap,&st,&en);
for(i=0;i<n;i++) for(j=0;j<=cap;j++) mincost[i][j]=inf;
w=bfs();
if(w==-1) puts("impossible");
else printf("%d\n",w);
}
}
return 0;
}


poj3635---BFS