惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
月光博客
月光博客
腾讯CDC
Engineering at Meta
Engineering at Meta
博客园 - Franky
Vercel News
Vercel News
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
GbyAI
GbyAI
B
Blog
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog

博客园 - zy_nic

SRM 424 div1 900 ProductOfPrices 翻译 .. emacs的c++mode contest contest 我的2-sat模板 Solution to GCJ Practice Contest Problem C, Cycles 约瑟夫问题的数学方法 我的模板 图的应用 今天的比赛 关于建图的 死老鼠安装成功 pku3141 先帖个题目上来 hnu 11028 hnu 11015 joj 儿死三八 pku 1273
pku3411
zy_nic · 2007-10-03 · via 博客园 - zy_nic

dfs搜索,last记录上次到某城市的已经能够预付的路,位压缩

#include <iostream>
using namespace std;

int ans;

int a[11],b[11],c[11],p[11],r[11],last[11];

int n,m;

int const inf = 1000000000;

void DFS(int now,int state,int mon)
{

    
int i,j;

    
if (now==&& mon<ans) ans=mon;

    
int sta=state;

    
for (i=0;i<m;i++)

        
if (c[i]==now)

            state
=state | (1<<i);

    
for (i=1;i<=n;i++)

        
if (last[i]!=state || last[i]==-1)
        
{

            
int mm=inf;

            
for (j=0;j<m;j++)
                
if (a[j]==now && b[j]==i)

                  
if ( (1<<j) & state )
                  
{
                      
if (p[j]<mm) mm=p[j];
                  }

                  
else
                  
{
                       
if (r[j]<mm) mm=r[j];
                  }


            
           
if (mm==inf) continue;

           
int ori=last[i];

           last[i]
=state;

           DFS(i,state,mon
+mm);

           last[i]
=ori;

        }


    state
=sta;
}


int main()
{
    
while (cin>>n>>m)
    
{
        
int i;

        
for (i=0;i<m;i++)
            cin
>>a[i]>>b[i]>>c[i]>>p[i]>>r[i];


        ans
=inf;

        memset(last,
-1,sizeof(last));

        last[
1]=0;

        DFS(
1,0,0);

        
if (ans==inf) cout<<"impossible"<<endl;
        
else

        cout
<<ans<<endl;
    }


    
return 0;



}