Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

SPFA用队列超时之后,改为栈就AC了,于是突发奇想,写了个记忆化搜索dp,AC了,贴代码

Posted by weihaohaoren at 2014-06-09 00:36:58 on Problem 3159
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>

int n,m;
struct E
{
    int v;
    int x;
    int next;
}e[151000];
int ce;
int head[31000];
void addedge(int u,int v,int x)
{
    e[ce].v=v;
    e[ce].x=x;
    e[ce].next=head[u];
    head[u]=ce++;
}
int d[31000];
bool used[31000];
int dp(int u)
{
    if(used[u]) return d[u];
    if(u==1) return 0;
    used[u]=1;
    d[u]=100000000;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].v;
        int x=e[i].x;
        if(dp(v)+x<=d[u]) d[u]=dp(v)+x;
    }    
    return d[u];
}
int main()
{
	freopen("3159.txt","r",stdin);
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof head);
	while(m--)
	{
	    int a,b,c;
	    scanf("%d%d%d",&a,&b,&c);
	    addedge(b,a,c);
	}		
	dp(n);
	printf("%d\n",d[n]);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator