poj 1724 ROADS 加限制的最短路
来源:互联网 发布:node.js增加json数据 编辑:程序博客网 时间:2024/04/28 14:55
//SPFA+二维数组记录状态
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=10005;
int n, m, d[105][maxn], pre[maxn], inqueue[maxn], cnt, money, count[maxn];
struct node{
int v, w, l, next;
}edge[maxn];
void add_edge(int s, int e, int w, int len){
edge[cnt].v=e;
edge[cnt].w=w;
edge[cnt].l=len;
edge[cnt].next=pre[s];
pre[s]=cnt;
cnt++;
}
void init(){
int i, x, y, c, len;
cnt=1;
scanf("%d%d%d", &money, &n, &m);
for(i=1; i<=n; i++){
pre[i]=-1;
count[i]=0;
}
for(i=0; i<m; i++){
scanf("%d%d%d%d", &x, &y, &len, &c);
add_edge(x, y, c, len);
}
}
bool spfa(int st){
int i, j, head, s;
memset(d, 0 , sizeof(d));
for(i=2; i<=n; i++){
inqueue[i]=0;
for(j=0; j<=money; j++)
d[i][j]=INF;
}
queue<int > q;
inqueue[st]=1;
q.push(st);
while(!q.empty()){
head=q.front();
q.pop();
inqueue[head]=0;
s=pre[head];
while(s!=-1){
for(i=edge[s].w; i<=money; i++){
if(d[edge[s].v][i]>d[head][i-edge[s].w]+edge[s].l){
d[edge[s].v][i]=d[head][i-edge[s].w]+edge[s].l;
if(!inqueue[edge[s].v]){
count[edge[s].v]++;
if(count[edge[s].v]>=n)return false;
inqueue[edge[s].v]=1;
q.push(edge[s].v);
}
}
}
s=edge[s].next;
}
}
return true;
}
int main(){
//freopen("1.txt", "r", stdin);
int i, T;
int ans;
init();
if(spfa(1)){
ans=INF;
for(i=0; i<=money; i++){
if(d[n][i]<ans)
ans=d[n][i];
}
}
else ans=-1;
printf("%d\n", ans);
return 0;
}
- poj 1724 ROADS 加限制的最短路
- 【POJ 1724】Roads (限制最短路)
- POJ 1724 - ROADS 有限制的最短路,A*
- POJ 1724 ROADS (有限制的最短路)
- POJ1724 ROADS(加限制条件的最短路,深搜)
- Poj 1724 ROADS【二维限制最短路+SPFA】
- 【poj】【费用限制最短路】ROADs
- POJ 1724 ROADS 最短路
- poj 1724(有限制的最短路)
- [POJ 1724] ROADS (有约束条件的最短路)
- POJ 1724 ROADS 带花费的最短路
- POJ 1724 ROADS 有约束的最短路问题, BFS
- POJ 1724 ROADS 有条件最短路
- POJ 1724 ROADS(bfs最短路)
- 【POJ】1724 ROADS 二维最短路
- poj 1724 ROADS条件限制下的最短路径
- Roads带限制条件的最短路算法
- poj_1724 ROADS(有费用限制的最短路)
- win7 系统服务
- java IO操作:字节流与字符流区别
- Mac
- 再读西游,前序
- 在模块内对内存的分配过早优化的缺点
- poj 1724 ROADS 加限制的最短路
- 如何取得checkbox复选框中选中的值
- 快两年了
- windows下面path环境变量的设置
- 选择排序
- 各种字符编码详解(ascii,utf-8,unicode,gbk,gb2312,iso8859-1等)
- 第四周 任务一
- js子窗体传值给父窗体
- 第三周实验报告2