前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 3411 Paid Roads

POJ 3411 Paid Roads

作者头像
全栈程序员站长
发布2022-07-06 11:01:18
1650
发布2022-07-06 11:01:18
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

主题链接~~>

做题情绪:先前看过一次,感觉做多了状态压缩之后。再做这题就非常顺手了。

解题思路: BFS + 状态压缩

一个城市能够走多次,so ~ > 须要用状态压缩。開始用了优先队列可是后来发现不能够用。当中例子用优先队列就过不了,于是把标记数组改为记录达到某个城市达到某种 状态的最小花费,假设搜到某个状态花费比当前状态小。更新标记数组的花费。就继续搜下去。否则不向下搜,这样搜出全部的情况就能够了。

(注意推断的时候要推断终点城市是否在状态里,開始有点脑残了推断是否全部城市都到达了)。

代码:

代码语言:javascript
复制
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std  ;
#define INT __int64
#define L(x)  (x * 2)
#define R(x)  (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;  // 问 mod  HDU 5063
const double PI = acos(-1.0) ;
const int mod = 1e9 + 7 ;
const int MY = 10 + 5 ;
const int MX = (1<<10) + 5 ;
int n ,m ;
int vis[MY][MX] ;
struct node
{
    int c1 ,c2 ;
}mp[MY][MY][MY] ;
struct NODE
{
    int x ,cost ,key ;
} ;
int bfs(int x)
{
    int ans = INF ;
    queue<NODE> q ;
    NODE curt ,next ;
    curt.x = x ;  // 终于到达的点
    curt.cost = 0 ;  // 花费
    curt.key = 1 ;  // 状态
    vis[0][1] = 0 ;
    q.push(curt) ;
    while(!q.empty())
    {
        curt = q.front() ;
        q.pop() ;
        if(curt.key &(1<<(n-1)))
            ans = min(ans ,curt.cost) ;
        for(int i = 0 ;i < n ; ++i)
          for(int j = 0 ;j < n ; ++j)
           if(mp[curt.x][i][j].c1 != -1)
           {
              int costA = INF ,costB = INF ;
              next.key  = curt.key ;
              if(next.key&(1<<j)) // 预支点在里面
                   costA = curt.cost + mp[curt.x][i][j].c1 ; // 原先的花费加上当前花费
              costB = curt.cost + mp[curt.x][i][j].c2 ;
              next.cost = min(costA ,costB) ;
              next.x = i ;
              if(!(next.key&(1<<i)))
                     next.key |= (1<<i) ;
              if(vis[i][next.key] > next.cost)
              {
                  vis[i][next.key] = next.cost ;
                  q.push(next) ;
              }
           }
    }
    return  ans ;
}
int main()
{
    //freopen("input.txt" ,"r" ,stdin) ;
    int u ,v ,t ,c1 ,c2 ;
    while(~scanf("%d%d" ,&n ,&m))
    {
        memset(mp ,-1 ,sizeof(mp)) ;
        for(int S = 0 ;S < (1<<n) ; ++S)
          for(int i = 0 ;i < n ; ++i)
             vis[i][S] = INF ;
        for(int i = 0 ;i < m ; ++i)
        {
            scanf("%d%d%d%d%d" ,&u ,&v ,&t ,&c1 ,&c2) ;
            u-- ; v-- ; t-- ;
            if(mp[u][v][t].c1 != -1)  // 防止重边(本题不知是否存在)
                    mp[u][v][t].c1 = min(mp[u][v][t].c1 ,c1) ;
            else    mp[u][v][t].c1 = c1 ;
            if(mp[u][v][t].c2 != -1)
                  mp[u][v][t].c2 = min(mp[u][v][t].c2 ,c2) ;
            else  mp[u][v][t].c2 = c2 ;
        }
        int mx = bfs(0) ;
        if(mx != INF)
                cout<<mx<<endl ;
        else    cout<<"impossible"<<endl ;
    }
    return 0 ;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117176.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档