题解【循环往复】
前排谢罪:本题由于描述和评分的问题作出了多次更改,给大家造成不便,对此深表歉意。。
upd2019-11-13:std出锅了QAQ感谢 @ticmis @fleur_de_lys @geyuntian 等大佬提供的hack数据~我还是太弱了。。。
比赛
题目点这里
这个题还是直接上正解吧。
我们需要把车站之间的到达关系转化为一张图。
如果在站
题目中
从这里就可以看出来了,这是一道缩点 +
记第
在把原图处理成
注意题目问的是从哪个起点开始可以使答案最优,所以我们在建
(以下表述中的编号均为新
这里我们用
容易得知,在初始化时,dp[i][ext[i]] = Q[i], cost[i][ext[i]] = P[i];其余的
我们设
首先,我们要确定一个状态能否转移。从上可见,
q0 = dp[x][0]; if(q0) q0 += Q[y];
q1 = dp[x][1]; if(q1) q1 += Q[y];
p0 = cost[x][0]; if(p0) p0 += P[y];
p1 = cost[x][1]; if(p1) p1 += P[y];
题目中
然后,我们考虑如何设计状态转移方程。
显然,在
if(ext[y] == 0){
if(iodp[y][0] < q0)
dp[y][0] = q0,
cost[y][0] = p0;
else if(dp[y][0] == q0 && cost[y][0] > p0)
cost[y][0] = p0;
if(dp[y][1] < q1)
dp[y][1] = q1,
cost[y][1] = p1;
else if(dp[y][1] == q1 && cost[y][1] > p1)
cost[y][1] = p1;
}
(别忘了同时维护
而在
if(ext[y] == 1){
(q0 > q1) ? (qx = q0, pm = p0) : (qx = q1, pm = p1);
(q0 == q1) ? (pm = min(p0, p1)) : (p0);
if(dp[y][1] < qx)
dp[y][1] = qx,
cost[y][1] = pm;
else if(dp[y][1] == qx && cost[y][1] > pm)
cost[y][1] = pm;
}
令
至于
需要注意的是,在
下面的数据就可以卡掉贪心做法:
5
1 1 1 1 1
2 1 1 1 2
2
1 2
2 3 0
1 3 0
0 1 0 0 1
正确的路线是
(再次感谢各位
时间复杂度
(为阅读方便特意调整了代码,因为本人码风过于奇葩。。)
#include<algorithm>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
const int MAXN = 100010, MAXM = 200010;
int n, m; LL p[MAXN], q[MAXN]; char eis[MAXN];
struct graph{
int heads[MAXN], cnt, next[MAXM], to[MAXM];
void addE(int x, int y){
next[++cnt] = heads[x]; to[cnt] = y; heads[x] = cnt;
}
void dfs1(int x);
}; graph E, E2;
int dfn[MAXN], low[MAXN], cnt, sta[MAXM], top, bel[MAXN], scn;
char vis[MAXN];
const int MAXC = 100010;
LL P[MAXC], Q[MAXC]; char ext[MAXC]; int ind[MAXC], oud[MAXC];
void graph::dfs1(int x){ // tarjan 过程
int y, e;
dfn[x] = low[x] = ++cnt;
sta[++top] = x; vis[x] = 1;
for(e = heads[x]; e; e = next[e]){
y = to[e];
if(!dfn[y]){
dfs1(y);
low[x] = min(low[x], low[y]);
}
else if(vis[y]){
low[x] = min(low[x], dfn[y]);
}
}
if(dfn[x] == low[x]){
++scn; P[scn] = Q[scn] = ext[scn] = 0;
while(top){
y = sta[top--];
vis[y] = 0; bel[y] = scn;
P[scn] += p[y]; Q[scn] += q[y];
if(eis[y]) ext[scn] = 1;
if(x == y) break;
}
}
}
void reconstruct(){
int x, y, e;
for(x = 1; x <= n; ++x)
for(e = E.heads[x]; e; e = E.next[e]){
y = E.to[e];
if(bel[x] != bel[y]){
++ind[bel[x]]; ++oud[bel[y]];
E2.addE(bel[y], bel[x]);
}
}
}
LL cost[MAXC][2], dp[MAXC][2];
int ansy; LL ansq, ansp; char anse;
void dynamic(){
int x, y, e, l = 0, r = 0;
memset(cost, 0x3f, sizeof cost);
for(x = 1; x <= scn; ++x)
if(!ind[x]){
sta[r++] = x;
cost[x][ext[x]] = P[x];
dp[x][ext[x]] = Q[x];
}
LL p0, p1, q0, q1, pm, qx;
while(l < r){
x = sta[l++];
for(e = E2.heads[x]; e; e = E2.next[e])
{
y = E2.to[e]; --ind[y];
if(!ind[y]) sta[r++] = y;
q0 = dp[x][0]; if(q0) q0 += Q[y];
q1 = dp[x][1]; if(q1) q1 += Q[y];
p0 = cost[x][0]; if(p0) p0 += P[y];
p1 = cost[x][1]; if(p1) p1 += P[y];
if(ext[y]){
(q0 > q1) ? (qx = q0, pm = p0) : (qx = q1, pm = p1);
(q0 == q1) ? (pm = min(p0, p1)) : (p0);
if(dp[y][1] < qx)
dp[y][1] = qx,
cost[y][1] = pm;
else if(dp[y][1] == qx && cost[y][1] > pm)
cost[y][1] = pm;
}
else{
if(dp[y][0] < q0)
dp[y][0] = q0,
cost[y][0] = p0;
else if(dp[y][0] == q0 && cost[y][0] > p0)
cost[y][0] = p0;
if(dp[y][1] < q1)
dp[y][1] = q1,
cost[y][1] = p1;
else if(dp[y][1] == q1 && cost[y][1] > p1)
cost[y][1] = p1;
}
}
}
anse = 0; ansq = 0; ansp = 0x3f7f7f7f7f7f7f7f;
for(x = 1; x <= scn; ++x){
if(oud[x]) continue;
if(dp[x][1]){
if(!anse){ // 在最后求答案的时候贪心,注意优先级
ansy = x;
anse = 1;
ansq = dp[x][1];
ansp = cost[x][1];
}
else if(ansq < dp[x][1]){
ansy = x;
ansq = dp[x][1];
ansp = cost[x][1];
}
else if(ansq == dp[x][1] && ansp > cost[x][1]){
ansy = x;
ansp = cost[x][1];
}
}
else{
if(anse) continue;
if(ansq < dp[x][0]){
ansy = x;
ansq = dp[x][0];
ansp = cost[x][0];
}
else if(ansq == dp[x][0] && ansp > cost[x][0]){
ansy = x;
ansp = cost[x][0];
}
}
}
}
int main(){
int i, x, y;
scanf("%d", &n);
for(i = 1; i <= n; ++i) scanf("%lld", &p[i]);
for(i = 1; i <= n; ++i) scanf("%lld", &q[i]);
scanf("%d", &m);
for(i = 1; i <= m; ++i) scanf("%d", &sta[i]);
for(i = 1; i <= m; ++i)
while(1){
scanf("%d", &x);
if(!x) break;
y = (x+sta[i]-1)%n+1;
E.addE(x, y);
}
for(i = 1; i <= n; ++i) scanf("%d", &eis[i]);
memset(dfn, 0x00, sizeof dfn);
memset(vis, 0x00, sizeof vis);
top = cnt = 0;
for(i = 1; i <= n; ++i)
if(!dfn[i]) E.dfs1(i);
reconstruct();
dynamic();
for(i = 1; i <= n; ++i)
if(bel[i] == ansy) break;
if(anse)
printf("%d %lld %lld\n", i, ansq, ansp);
else
printf("-%d -%lld -%lld\n", i, ansq, ansp);
return 0;
}
upd2019-11-16 题解再次更正,感谢@00ffcc 的提醒,之前改题解时粘错代码了。。。