POJ 1724 인접표 + 우선 대기열 + spfa ()
3467 단어 최단로
ROADS
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 7021
Accepted: 2623
Description
N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find
the shortest path from the city 1 to the city N
that he can afford with the amount of money he has.
Input
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
Notice that different roads may have the same source and destination cities.
Output
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
4
11
ac 코드:#include
#include
#include
#include
using namespace std;
const int N=110;
const int M=10010;
struct edge{
int rp,value,cost,next;
}ee[M];
struct point{
int sp,len,fee;
bool operator qq;
int totalmoney,numcity,numpath,num,head[M],dis[N];
void addedge(int x,int y,int ll,int v){
ee[num].rp=y;
ee[num].value=ll;
ee[num].cost=v;
ee[num].next=head[x];
head[x]=num++;
}
void spfa(){
while(!qq.empty())
qq.pop();
p1.fee=0;p1.len=0;p1.sp=1;
qq.push(p1);
while(!qq.empty()){
p2=qq.top();
qq.pop();
int x=p2.sp;
if(x==numcity){
printf("%d
",p2.len);
return;
}
for(int i=head[x];i!=-1;i=ee[i].next){
int y=ee[i].rp;
if(p2.fee+ee[i].cost<=totalmoney){
p1.fee=p2.fee+ee[i].cost;
p1.len=p2.len+ee[i].value;
p1.sp=y;
qq.push(p1);
}
}
}
printf("-1
");
return ;
}
int main(){
//freopen("1.txt","r",stdin);
while(~scanf("%d",&totalmoney)){
num=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&numcity,&numpath);
int x,y,ll,v;
for(int i=1;i<=numpath;++i){
scanf("%d%d%d%d",&x,&y,&ll,&v);
addedge(x,y,ll,v);
}
spfa();
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Uva10986-Sending email텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.