- 晴天小猪历险记之Hill
- 2014-03-21 17:35:30 @
//
// main.cpp
// vijos 1006
//
// Created by yrh on 3/21/14.
// Copyright (c) 2014 匿名的回忆. All rights reserved.
//
/*
5
1
2 3
4 5 6
10 1 7 8
1 1 4 5 6
10
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <stdlib.h>
#include <math.h>
#include <time.h>
using namespace std;
const long max_n=555555,max_m=2222222,maxint=1111111111;
long cost[max_n],now[max_n],f[max_n];
long pre[max_m],son[max_m];
long road=0,n=0,p=0;
bool use[max_n];
void debug(void)
{
long i,j=12;
i=now[j];
printf("cost[%ld]=%ld\n",j,cost[j]);
while (i>0)
{
printf("cost[%ld]=%ld,f=%ld\n",son[i],cost[son[i]],f[son[i]]);
i=pre[i];
}
}
void connect(long a,long b)
{
road++;
pre[road]=now[a];
now[a]=road;
son[road]=b;
}
void spfa(long start)
{
long head=0,tail=1,list[max_n];
memset(list, 0, sizeof(list));
memset(use, false, sizeof(use));
list[tail]=start;
use[start]=true;
while (head!=tail)
{
head=(head % p)+1;
long fa=list[head];
road=now[fa];
while (road>0)
{
long so=son[road];
if (f[so]>f[fa]+cost[so])
{
f[so]=f[fa]+cost[so];
if (!use[so])
{
use[so]=true;
tail=(tail % p)+1;
list[tail]=so;
}
}
road=pre[road];
}
use[fa]=false;
}
}
void init(void)
{
memset(now, 0, sizeof(now));
long i,j;
cin >> n;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
{
p++;
cin >> cost[p];
f[p]=maxint;
if (i==n&&j==1) f[p]=cost[p];
if (i>1)
{
if (2==i)
{
if (1==j)
{
connect(p, p+1);
connect(p, p-1);
} else
if (2==j)
{
connect(p, p-1);
connect(p, p-i);
}
} else
{
if (j>1&&j<i)
{
connect(p, p-1);
connect(p, p+1);
connect(p, p-i);
connect(p, p-i+1);
}else
{
if (1==j)
{
connect(p, p+i-1);
connect(p, p+1);
connect(p, p-1);
connect(p, p-i+1);
}else
{
connect(p, p-1);
connect(p, p-i);
connect(p, p-i-i+2);
connect(p, p-i+1);
}
}
}
}
}
}
int main(void)
{
init();
// debug();
spfa(p-n+1);
cout << f[1];
return 0;
}
1 条评论
-
940254533 LV 8 @ 2014-03-21 21:01:03
- 1