hdu - 4396 More lumber is required(二维最短路)

题目:

More lumber is required


题意:给N个节点,M条双向边,M行a,b,c表示一条边。每经过一条边可以获得10个单位,边可以重复走,最后一行表起点和终点,和所需要的最少单位,求获得符合数量单位后的路径最小值


思路:用spfa寻找最短路,由于可以重复走,所以必须多设一维来判断当前这个点处于什么状态,同状态点之间不能相互走


代码:

spfa实现

//#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 0x1fffffff;
const int MAXN = 1000000+100;
#define eps 1e-14
const int mod = 95041567;

struct edge
{
    int now;
    int next_edge;
    int w;
}e[1000000+100];

int head[5050];
int cnt=0;

void add(int x , int y , int v)
{
    e[cnt].now=y;
    e[cnt].next_edge = head[x];
    e[cnt].w = v;
    head[x] = cnt++;
}

int vis[5050][100];
int low[5050][100];
int dis;
int max_tot;

struct node
{
    int id;
    int state;

    node(int x,int y):id(x),state(y) {}
};

int s,ed,tot;

void spfa(int n , int src)
{

    for(int i = 0 ; i <= n ; i++)
            for(int j = 0 ; j <= tot ; j++)
                low[i][j] = INF;

    queue<node> que;
    que.push(node(src,0));
    low[src][0]=0;
    vis[src][0]=1;

    while(!que.empty())
    {
        node cur = que.front();
        que.pop();
        vis[cur.id][cur.state]=0;

        if(cur.id == ed && cur.state == tot)
           dis = min(dis,low[ed][tot]);

        for(int i = head[cur.id] ; i != -1 ; i = e[i].next_edge)
        {
            int now = e[i].now;
            int state = cur.state+1;
            if(state > tot)                 //重要判断,只要state到达tot,最优的做法必定是直接从当前点寻最短路到达终点
                state = tot;
            if(low[cur.id][cur.state] + e[i].w < low[now][state])
            {
                low[now][state] = low[cur.id][cur.state] + e[i].w;
                if(!vis[now][state])
                {
                    que.push(node(now,state));
                    vis[now][state]=1;
                }
            }
        }
    }
}

int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);

    int N , M;

    while(~scanf("%d%d",&N,&M))
    {

        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));

        dis=INF;
        max_tot=-1;

        for(int i = 0 ; i < M ; i++)
        {
            int a,b,v;
            scanf("%d%d%d",&a,&b,&v);

            add(a,b,v);
            add(b,a,v);
        }


        scanf("%d%d%d",&s,&ed,&tot);

        tot = tot%10 ? tot/10+1 : tot/10;

        spfa(N,s);

        if(dis == INF)
            puts("-1");
        else
            printf("%d\n",dis);

    }

    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值