POJ 1724 인접표 + 우선 대기열 + spfa ()

3467 단어 최단로
그런데 이 문제는 어제 ac를 했어야 했는데 비극적인 것은 어제 컴퓨터에 문제가 좀 생겨서 지체했어. 오늘 코드를 다시 쓴 후에 ac가 됐어.이 문제는 우선 대기열을 사용하고 그림은 인접표로 저장하며 spfa()를 사용하면 시간이 많이 줄어든다.제목:
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 : 
  • S is the source city, 1 <= S <= N 
  • D is the destination city, 1 <= D <= N 
  • L is the road length, 1 <= L <= 100 
  • T is the toll (expressed in the number of coins), 0 <= T <=100

  • 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; }

    좋은 웹페이지 즐겨찾기