回家 Bessie Come Home

https://www.luogu.org/problemnew/show/P1529

题解:SPFA

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int n,m,k,p,l,r,w,u,v;
int ans,cnt,flag,temp,sum;
int dis[N];
bool vis[N];
char s,t;
struct node{
    int u,v,w;
    node(){};
    node(int u,int v,int w):u(u),v(v),w(w){}
};
vector<node>G[N];
queue<int>q;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        cin>>s>>t>>w;
        u=s+(islower(s)? -'a':26-'A')+1;
        v=t+(islower(t)? -'a':26-'A')+1;
        G[u].push_back({u,v,w});
        G[v].push_back({v,u,w});
    }
    for(int i=1;i<=52;i++)dis[i]=INF;
    vis[52]=1;
    q.push(52);
    dis[52]=0;
    while(!q.empty()){
        int u=q.front();
        for(int i=0,j=G[u].size();i<j;i++){
            node tmp=G[u][i];
            if(dis[tmp.v]>dis[u]+tmp.w){
                dis[tmp.v]=dis[u]+tmp.w;
                //ans=max(ans,dis[tmp.v]);
                if(!vis[tmp.v]){
                    q.push(tmp.v);
                    vis[tmp.v]=1;
                }
            }
        }
        vis[u]=0;
        q.pop();
    }
    int pos=26;
    ans=INF;
    for(int i=27;i<52;i++){
        if(dis[i]<ans){
            pos=i-27;
            ans=dis[i];
        }
    }
    cout<<char(pos+'A')<<" "<<ans<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

oppo 应用软开 22*15+0.5*12
拿到了ssp完美:真的坎坷,但是你至少拿到这么多offer了!
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务