Chemical Reaction UVA – 10604

Miku

这道题样例错了!!!

正确解决方法 ⇒ 写一个搜索 ⇒ TLE ⇒进行记忆化改造 ⇒ Ac

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =15;
char c;
int dp[maxn][maxn][maxn][maxn][maxn][maxn];
int he[maxn][maxn],tu[maxn][maxn];
int sum[maxn];
int maxx;
int n,k;
int t;
int cnt;
int q;
int dfs(){
    int &ans=dp[sum[1]][sum[2]][sum[3]][sum[4]][sum[5]][sum[6]];
    if(ans!=maxx)
    return ans;
    if(cnt==q-1)
    return ans=0;
    for(int i=1;i<=n;++i){
        if(sum[i]){
            sum[i]--;
            for(int j=1;j<=n;++j){
                if(sum[j]){
                    sum[j]--;
                    cnt++;
                    sum[tu[i][j]]++;
                    ans=min(ans,dfs()+he[i][j]);
                    cnt--;
                    sum[tu[i][j]]--;
                    sum[j]++;
                }
            }
            sum[i]++;
        }
    }
    return ans;

}
int main(){
    scanf("%d",&t);
    while(t--){
        memset(dp,0x7f,sizeof(dp));
        memset(sum,0,sizeof(sum));
        cnt=0;
        memset(he,0,sizeof(he));
        memset(tu,0,sizeof(tu));
        maxx=dp[1][1][1][1][1][1];
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                scanf("%d%d",&tu[i][j],&he[i][j]);
            }
        }
        scanf("%d",&q);
        for(int i=1;i<=q;++i){
            scanf("%d",&k);
            sum[k]++;
        }
        cout<<dfs()<<endl;
        cin>>c;
    }
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇