解题报告 Paid Roads

Paid Roads

【题目描述】

这里有一个地图,嗯~准确的说是一个藏宝图。你在1号点,宝藏在n号点,所有的点编号1~n,这块宝底的地形是很奇怪的,每两个点之间有两条通路,两个通路的长度是不一样的,可能会有一条比较短,你可以任选一条,但是其中一条你只有在之前经过某个点的时才能通行,就好像这条路的通行证在那个点上一样。现在想知道怎么样走才能以最短的路程到达藏宝点。

【输入格式】

第一行两个用空格隔开的整数n和m分别表示节点的编号个数和该藏宝点路径条数。

第二行到第m+1行每行5个整数a,b,c,la,lb描述从a点到b点的有向路,la表示之前经过c后,可以从a到b的路径长度,lb表示随时都可以同行的a到b的路径长度。

【输出格式】

一行一个整数表示的是从1到n的最短路程。如果没有路输出‘impossible’。

【输入样例】

4 5

1 2 1 10 10

2 3 1 30 50

3 4 3 80 80

2 1 2 10 10

1 3 2 10 50

【输出样例】

110

【数据范围】

N,m<=10

la,lb<=maxlongint

 

 

这个题要用深搜。

因为宽搜是走不了环的,但这个题需要走环,但是,一个环只能走一次,走这个环是为了减少某一条路的权值,所以去这个环上拿了通行证,就不用再来一遍了,标记判断。

 

program lonely;

 

  type

    nod=record

      y,k,la,lb,next:longint;

    end;

 

  var

    map:array[0..100] of nod;

    v:Array[0..100] of longint;

    i,m,x,y,k,la,lb,ans,len,n:longint;

    first:array[0..100] of longint;

    ff:boolean;

 

  procedure dfs(x:longint);

    var

      t,y,temp,ll:longint;

    begin

      if x=n then

        begin

          ff:=true;

          if len<ans then ans:=len;

          exit;

        end;

      if len>ans then exit;

      t:=first[x];

      while t<>0 do

        begin

          y:=map[t].y;

          if v[y]<=4 then

            begin

              temp:=map[t].k;

              ll:=map[t].lb;

              if v[temp]>0 then

                if map[t].la<ll then ll:=map[t].la;

              inc(v[y]);

              len:=len+ll;

              dfs(y);

              len:=len-ll;

              dec(v[y]);

            end;

          t:=map[t].next;

        end;

    end;

 

  begin

    assign(input,'roads.in');

    reset(input);

    assign(output,'roads.out');

    rewrite(output);

 

    readln(n,m);

    for i:=1 to m do

      begin

        readln(x,y,k,la,lb);

        map[i].y:=y;

        map[i].k:=k;

        map[i].la:=la;

        map[i].lb:=lb;

        map[i].next:=first[x];

        first[x]:=i;

      end;

    fillchar(v,sizeof(v),0);

    ans:=maxlongint >> 1;

    ff:=false;

    len:=0;

    inc(v[1]);

    dfs(1);

    if not ff then writeln('impossible') else writeln(ans);

 

    close(input);

    close(output);

  end.

 

原文地址:https://www.cnblogs.com/SueMiller/p/2226787.html