• hdu Free DIY Tour


    http://acm.hdu.edu.cn/showproblem.php?pid=1224

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 #define maxn 5600
      6 using namespace std;
      7 const int inf=-600000;
      8 int e,n,m,a,b;
      9 int head[maxn*4];
     10 int dis[maxn*4];
     11 bool vis[maxn*4];
     12 int pre[maxn*4];
     13 int cnt[maxn*4];
     14 int val[maxn*4];
     15 
     16 struct node
     17 {
     18     int u,v,w,next;
     19 } p[maxn*4];
     20 
     21 void add(int u,int v,int w)
     22 {
     23     p[e].u=u;
     24     p[e].v=v;
     25     p[e].w=w;
     26     p[e].next=head[u];
     27     head[u]=e++;
     28 }
     29 
     30 bool relax(int u,int v,int w)
     31 {
     32     if(dis[v]<dis[u]+w)
     33     {
     34         dis[v]=dis[u]+w;
     35         return true;
     36     }
     37     return false;
     38 }
     39 bool spfa(int src)
     40 {
     41     memset(cnt,false,sizeof(cnt));
     42     memset(vis,false,sizeof(vis));
     43     queue<int>q;
     44     for(int i=1; i<=n; i++) dis[i]=inf;
     45     for(int i=1; i<=n; i++) pre[i]=0;
     46     dis[src]=0;
     47     vis[src]=true;
     48     q.push(src);
     49     while(!q.empty())
     50     {
     51         int uu=q.front();
     52         q.pop();
     53         vis[uu]=false;
     54         for(int i=head[uu]; i!=-1; i=p[i].next)
     55         {
     56             if(relax(uu,p[i].v,p[i].w))
     57             {
     58                 //if((++cnt[p[i].v])>n) return false;
     59                 pre[p[i].v]=uu;
     60                 if(!vis[p[i].v])
     61                 {
     62                     vis[p[i].v]=true;
     63                     q.push(p[i].v);
     64                 }
     65             }
     66         }
     67     }
     68     return true;
     69 }
     70 void inti()
     71 {
     72     memset(head,-1,sizeof(head));
     73     e=0;
     74 }
     75 
     76 void pritn(int c)
     77 {
     78     if(pre[c]!=0)
     79         pritn(pre[c]);
     80     printf("%d->",c);
     81     return ;
     82 }
     83 int main()
     84 {
     85     int t;
     86     scanf("%d",&t);
     87     for(int i=1; i<=t; i++)
     88     {
     89         inti();
     90         scanf("%d",&n);
     91         for(int j=1; j<=n; j++)
     92         {
     93             scanf("%d",&val[j]);
     94         }
     95         n++;
     96         val[n]=0;
     97         scanf("%d",&m);
     98         for(int j=0; j<m; j++)
     99         {
    100             scanf("%d%d",&a,&b);
    101             add(a,b,val[b]);
    102         }
    103         spfa(1);
    104         printf("CASE %d#
    ",i);
    105         printf("points : %d
    ",dis[n]);
    106         printf("circuit : ");
    107         pritn(pre[n]);
    108         printf("1
    ");
    109         if(i!=t)
    110             printf("
    ");
    111     }
    112     return 0;
    113 }
    View Code
  • 相关阅读:
    Netty实现客户端和服务端通信简单例子
    上拉电阻的作用
    c语言常量指针赋值给变量指针导致警告
    修改ultisnips的默认键
    为debian8.2更换官方源
    linux下添加用户到sudo组
    用rfkill命令管理蓝牙和wifi
    用platformio编写arduino程序
    ubuntu下gcc-avr安装
    UNIX环境高级编程(第三版)关于apue.h的用法
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3673936.html
Copyright © 2020-2023  润新知