Dwarf Tower

 

骇人听闻的数据:

源代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct Node
{
    int T,X,Y;
}Edge[100001];
int n,m,i[10001];
int main()
{
    scanf("%d%d",&n,&m);
    for (int a=1;a<=n;a++)
      scanf("%d",&i[a]);
    for (int a=1;a<=m;a++)
    {
        scanf("%d%d%d",&Edge[a].T,&Edge[a].X,&Edge[a].Y);
        i[Edge[a].T]=min(i[Edge[a].X]+i[Edge[a].Y],i[Edge[a].T]);
    }
    for (int a=m;a>=1;a--)
      i[Edge[a].T]=min(i[Edge[a].X]+i[Edge[a].Y],i[Edge[a].T]);
    printf("%d",i[1]);
    return 0;
}

/*
    AC就是AC了,没有理由。
    数据水也不至于水到这种地步吧!
*/

 

75分乱搞DFS:

源代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Node
{
    int X,Y;
    Node(int t1,int t2)
    {
        X=t1;
        Y=t2;
    }
};
vector <Node> List[10001];
int m,n,i[10001];
bool Vis[10001]={0};
void DFS(int t) //倒序记忆化DFS。
{
    Vis[t]=true;
    int L=List[t].size();
    for (int a=0;a<L;a++)
    {
        int t1=List[t][a].X;
        int t2=List[t][a].Y;
        if (!Vis[t1])
          DFS(t1);
        if (!Vis[t2])
          DFS(t2);
        i[t]=min(i[t],i[t1]+i[t2]);
    }
}
int main() //可以发现,在树套树的时候,就会出现错误。
{
    scanf("%d%d",&n,&m);
    for (int a=1;a<=n;a++)
      scanf("%d",&i[a]);
    for (int a=0;a<m;a++)
    {
        int t,t1,t2;
        scanf("%d%d%d",&t,&t1,&t2);
        List[t].push_back(Node(t1,t2));
    }
    DFS(1);
    printf("%d",i[1]);
    return 0;
}

 

SPFA正解:

源代码:

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#define LL long long
using namespace std;
struct Node
{
    LL X,Y;
    Node(LL t1,LL t2)
    {
        X=t1;
        Y=t2;
    }
};
queue <int> Q;
vector <Node> List[10001];
LL m,n,i[10001];
bool In[10001];
int main() //挺动脑子的一道题。
{
    scanf("%lld%lld",&n,&m);
    for (LL a=1;a<=n;a++)
      scanf("%lld",&i[a]);
    for (LL a=1;a<=m;a++)
    {
        LL t,t1,t2;
        scanf("%d%d%d",&t,&t1,&t2);
        List[t1].push_back(Node(t,t2));
        List[t2].push_back(Node(t,t1));
    }
    for (LL a=1;a<=n;a++)
    {
        Q.push(a);
        In[a]=true;
    }
    while (!Q.empty()) //可以朦朦胧胧地看做是某一个神秘点到节点的距离,转换成一个类似于SPFA的BFS,不断更新就好了。
    {
        LL t=Q.front();
        Q.pop();
        In[t]=false;
        for (LL a=0;a<List[t].size();a++)
        {
            LL t1=List[t][a].X;
            LL t2=List[t][a].Y;
            if (i[t2]+i[t]<i[t1])
            {
                i[t1]=i[t2]+i[t];
                if (!In[t1])
                {
                    Q.push(t1);
                    In[t1]=true;
                }
            }
        }
    }
    printf("%lld",i[1]);
    return 0;
}
posted @ 2016-11-10 17:42  前前前世。  阅读(241)  评论(0编辑  收藏  举报