码迷,mamicode.com
首页 > 编程语言 > 详细

2018年全国多校算法寒假训练营练习比赛(第一场)E 恋与程序员

时间:2018-01-21 23:54:08      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:push   ems   its   log   style   bit   cost   --   训练营   

https://www.nowcoder.com/acm/contest/67/E

思路:

dfs

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))

const int N=105;
const int INF=0x3f3f3f3f;
int cnt=0;
int head[N];
int cost[N];
int d[N];
bool used[N];
bool vis[N];
struct edge{
    int to,w,next;
}edge[N];
void add_edge(int u,int v,int w){
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int ans=INF,c,k;
void dfs(int u){
    vis[u]=true;
    if(u==c){
        int tot=0;
        for(int i=1;i<=k;i++)if(used[i])tot+=cost[i];
        ans=min(ans,tot);
    }else{
        for(int i=head[u];~i;i=edge[i].next){
            if(!vis[edge[i].to])
            {
                bool t=used[edge[i].w];
                used[edge[i].w]=true;
                vis[edge[i].to]=true;
                dfs(edge[i].to);
                vis[edge[i].to]=false;
                used[edge[i].w]=t;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,e,a,b,u,v;
    while(cin>>n>>m>>k>>c){
        mem(head,-1);
        mem(vis,false);
        mem(used,false); 
        cnt=0;
        while(m--){
            cin>>u>>v>>e;
            add_edge(u,v,e);
        }
        for(int i=0;i<k;i++){
            cin>>a>>b;
            cost[a]=b;
        }
        ans=INF;
        dfs(1);
        cout<<ans<<endl;
    }
    return 0; 
} 
/*
6 7 5 6
2 3 2
4 3 3
1 2 1
1 5 4
4 6 5
1 4 2
5 6 3
1 100
3 422
2 210
5 107
4 38
*/

 

2018年全国多校算法寒假训练营练习比赛(第一场)E 恋与程序员

标签:push   ems   its   log   style   bit   cost   --   训练营   

原文地址:https://www.cnblogs.com/widsom/p/8325904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!