先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=;
int n,m,C1,C2,tot;
int Cmax;
int val[maxn];
struct Edge
{
int u,v,L;
}e[];
int f[];
vector<int>g[maxn];
int dis[][maxn]; int ans[maxn],cnt;
int sta[maxn]; int MIN1,MIN2; void init()
{
MIN1=MIN2=INF;
tot=;C1=;
for(int i=;i<=;i++) g[i].clear();
} void add(int a,int b,int c)
{
e[tot].u=a,e[tot].v=b,e[tot].L=c;
g[a].push_back(tot);
tot++;
} void read()
{
scanf("%d%d%d%d",&Cmax,&n,&C2,&m);
for(int i=;i<=n;i++) scanf("%d",&val[i]);
for(int i=;i<=m;i++)
{
int u,v,L; scanf("%d%d%d",&u,&v,&L);
add(u,v,L);
add(v,u,L);
}
} void SPFA(int f,int st)
{
for(int i=;i<=;i++) dis[f][i]=INF;
dis[f][st]=;
int flag[maxn]; memset(flag,,sizeof flag);
queue<int>Q; Q.push(st); flag[st]=;
while(!Q.empty())
{
int head= Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[f][head]+e[id].L<dis[f][e[id].v])
{
dis[f][e[id].v]=dis[f][head]+e[id].L;
if(flag[e[id].v]==)
{
Q.push(e[id].v);
flag[e[id].v]=;
}
}
}
}
} void dfs(int x,int num1,int num2,int deep)
{
sta[deep]=x; if(x==C2)
{
if(num2<MIN2)
{
MIN2=num2;
MIN1=num1;
for(int i=;i<=deep;i++) ans[i]=sta[i];
tot=deep;
} else if(num2==MIN2&&num1<MIN1)
{
MIN2=num2;
MIN1=num1;
for(int i=;i<=deep;i++) ans[i]=sta[i];
tot=deep;
}
return;
} for(int i=;i<g[x].size();i++)
{
if(f[g[x][i]]==) continue;
int to=e[g[x][i]].v; if(val[to]==Cmax/) dfs(to,num1,num2,deep+);
else if(val[to]>Cmax/) dfs(to,num1+val[to]-Cmax/,num2,deep+);
else if(val[to]<Cmax/)
{
if(num1>Cmax/-val[to]) dfs(to,num1+val[to]-Cmax/,num2,deep+);
else dfs(to,,num2+Cmax/-val[to]-num1,deep+);
}
}
} void work()
{
int len=dis[][C2]; for(int i=;i<tot;i++)
if(dis[][e[i].u]+e[i].L+dis[][e[i].v]==len)
f[i]=; dfs(,,,); printf("%d ",MIN2);
for(int i=;i<=tot;i++)
{
if(i<tot) printf("%d->",ans[i]);
else printf("%d ",ans[i]);
}
printf("%d\n",MIN1);
} int main()
{
init();
read();
SPFA(,C1);
SPFA(,C2);
work();
return ;
}

PAT (Advanced Level) 1018. Public Bike Management (30)的更多相关文章

  1. PTA (Advanced Level) 1018 Public Bike Management

    Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...

  2. Pat(Advanced Level)Practice--1018(Public Bike Management)

    Pat1018代码 题目描写叙述: There is a public bike service in Hangzhou City which provides great convenience t ...

  3. 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)

    题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...

  4. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  5. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  6. 1018. Public Bike Management (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...

  7. 1018 Public Bike Management (30)(30 分)

    时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...

  8. 1018 Public Bike Management (30 分)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  9. 1018 Public Bike Management (30分) 思路分析 + 满分代码

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

随机推荐

  1. [code]高精度运算

    数组存储整数,模拟手算进行四则运算 阶乘精确值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #includ ...

  2. CSS position 属性

    position: relative | absolute | static | fixed 参考网站:http://blog.csdn.net/dyllove98/article/details/8 ...

  3. hdu 5532

    input 1<=T<=2000 2<=n<=10e5 a1 a2 ... an 1<=ai<=10e5 最多二十个n>1000 output 能否从数组中移 ...

  4. hdu 5344 MZL's xor

    MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. weak和assign区别

    weak比assign多了一个功能,当对象消失后自动把指针变成nil haofanazenmeban[4002:406590] controller:<SecondViewController: ...

  6. 怎样解决VS2013模块对于SAFESEH 映像是不安全的

    今天在使用VS2013编译一个控制台应用程序时出现了:error LNK2026 模块对于 SAFESEH 映像是不安全的,按照以下步骤轻松解决了. 打开该项目的“属性页”对话框,然后单击“链接器”- ...

  7. Struts2中的验证框架

    通过注解的方式,可以让方法不用验证 @SkipValidation public String toRegView() { System.out.println("toRegView&quo ...

  8. android 定时器总结

    1:handler实现定时器的功能 Handler handler=new Handler(); //立即执行Runnable对象   public final boolean post(Runnab ...

  9. CodeForces 567B Berland National Library

    Description Berland National Library has recently been built in the capital of Berland. In addition, ...

  10. [原创] web_custom_request 与 Viewstate

    在用loadrunner对.net编写的website进行性能测试时,经常会遇上一些hidden fields,例如,CSRFTOKEN.VIEWSTATE.EVENTVALIDATION等,而对于这 ...