UVA 11367 - Full Tank
来源:互联网 发布:ubuntu 文本编辑命令 编辑:程序博客网 时间:2024/06/16 18:37
/*
最短路问题 可以分解为2种走法 1:原地加一单位油,2:到下一个地点。
*/
#include<cstdio>
#include<cstring>#include<queue>
#include<vector>
#define M 10010
using namespace std;
struct Edge
{
int from,to,dist;
Edge(int x,int y,int z)
{
from = x;
to = y;
dist = z;
}
};
struct P
{
int cost;
int u,f;
bool operator < (const P &a) const
{
return a.cost < cost;
}
};
int n,m,s,t,c;
const int INF = 1<<30;
vector<Edge> edges;
vector<int> G[M];
int dp[1010][1010],pr[1010],vis[1010][1010];
int init()
{
for(int i = 0; i < n; i++)
G[i].clear();
edges.clear();
}
int add(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
int tm = edges.size();
G[from].push_back(tm-1);
}
int bfs()
{
for(int i = 0; i < n; i++)
for(int j = 0; j <= 100; j++)
dp[i][j] = INF;
memset(vis,0,sizeof(vis));
priority_queue<P> q;
while(!q.empty())
q.pop();
P tmp,tp;
tmp.u = s;
tmp.f = 0;
tmp.cost = 0;
dp[s][0] = 0;
q.push(tmp);
while(!q.empty())
{
tmp = q.top();
q.pop();
vis[tmp.u][tmp.f] = 1;
if(tmp.u==t)
{
printf("%d\n",tmp.cost);
return 0;
}
if(tmp.f+1<=c&&!vis[tmp.u][tmp.f+1]&&(dp[tmp.u][tmp.f+1]>dp[tmp.u][tmp.f]+pr[tmp.u]))
{
dp[tmp.u][tmp.f+1]=dp[tmp.u][tmp.f]+pr[tmp.u];
tp.f = tmp.f+1;
tp.u = tmp.u;
tp.cost = dp[tmp.u][tmp.f+1];
q.push(tp);
}
for(int i = 0; i < G[tmp.u].size(); i++)
{
Edge &e = edges[G[tmp.u][i]];
int u = e.from,v = e.to,w = e.dist;
if(tmp.f>=w&&!vis[v][tmp.f-w]&&dp[v][tmp.f-w]>tmp.cost)
{
dp[v][tmp.f-w] = tmp.cost;
tp.u = v;
tp.f = tmp.f-w;
tp.cost = dp[v][tmp.f-w];
q.push(tp);
}
}
}
puts("impossible");
}
int main()
{
int x,y,z;
scanf("%d %d",&n,&m);
init();
for(int i = 0; i < n; i++)
scanf("%d",&pr[i]);
for(int i = 0; i < m; i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int kc;
scanf("%d",&kc);
while(kc--)
{
scanf("%d %d %d",&c,&s,&t);
bfs();
}
return 0;
}
- UVA 11367 - Full Tank
- uva 11367 - Full Tank?(dijkstra TLE)
- UVA 11367 Full Tank?(bfs最短路)
- UVA 11367 - Full Tank? dijkstra+DP
- uva 11367 - Full Tank?(最短路)
- UVA 11367 Full Tank? (dij+heap+dp)
- Full Tank? UVA
- Full Tank? UVA
- (beginer)最短路 UVA 11367 Full Tank?
- UVA 11367 - Full Tank?(最短路+DP)
- Uva 11367 Full Tank?(多状态最短路)
- POJ 3635 Full Tank?
- poj 3635 Full Tank?
- Poj3635 Full Tank?
- POJ 3635 Full Tank?
- POJ-3635-Full Tank?
- P – FULL TANK?
- poj3635 Full Tank
- 加载VxWorks时提示:Error loading file:errno = 0xd0003. Can't load boot file!
- 预防数据库受攻击
- Java中Thread的经典面向编程案例
- extra-code
- 个人开发者帐号--我是如何实现在另一台mac上真机调试的
- UVA 11367 - Full Tank
- 枚举,享元模式,反射基础
- JavaScript初学者最容易犯的错误(一)
- Linux进程地址空间的一步步探究
- 10个小技巧让你成为强大的程序员
- 题目:把一个有序整数数组放到二叉树。
- Away3d 4材质学习笔记
- BI笔记之---增量方式处理多维数据集
- 《C++面向对象程序设计》笔记