码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4360 As long as Binbin loves Sangsang spfa变形

时间:2015-01-27 09:28:00      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:spfa

题意:给定n个点m条边的无向图,每次必须沿着LOVE走,到终点时必须是完整的LOVE,且至少走出一个LOVE,问这样情况下最短

路是多少,在一样短情况下最多的LOVE个数是多少。注意:有自环!(见底下的数据)

思路:其实本质就是个最短路,用spfa就好。注意自环的特殊处理,详见代码:

/*********************************************************
  file name: hdu4360.cpp
  author : kereo
  create time:  2015年01月26日 星期一 17时37分23秒
*********************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sigma_size=26;
const int N=1500+50;
const int MAXN=15000+50;
const ll inf=1e18;
const double eps=1e-8;
const int mod=100000000+7;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define PII pair<int, int>
#define mk(x,y) make_pair((x),(y))
struct node{
    int u,label;
};
struct Edge{
    int v,w,id,next;
}edge[MAXN<<1];
int n,m,edge_cnt;
char str[5];
ll d[N][4];
int head[N],cnt[N][4],vis[N][4];
void init(){
    edge_cnt=0;
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int id){
    edge[edge_cnt].v=v; edge[edge_cnt].w=w;
    edge[edge_cnt].id=id; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;
}
void spfa(){
    queue<node>Q;
    node now; now.u=1; now.label=3;
    Q.push(now);
    for(int i=1;i<=n;i++)
        for(int j=0;j<4;j++)
            d[i][j]=inf,cnt[i][j]=0;
    d[1][3]=0; vis[1][3]=1;
    while(!Q.empty()){
        now=Q.front(); Q.pop();
        int u=now.u,label=now.label; vis[u][label]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v,w=edge[i].w,id=edge[i].id;
            if(id!=(label+1)%4)
                continue;
            int flag=0;
            if(d[v][id]>d[u][label]+w){
                d[v][id]=d[u][label]+w;
                int tmp=cnt[u][label];
                if(id == 3)
                    tmp++;
                cnt[v][id]=tmp; flag=1;
            }
            else if(d[v][id] == d[u][label]+w){
                int tmp=cnt[u][label];
                if(id == 3)
                    tmp++;
                if(tmp>cnt[v][id]){
                    cnt[v][id]=tmp; flag=1;
                }
            }
            else if(cnt[v][id] == 0){ //自环的特殊处理
                int tmp=cnt[u][label];
                if(id == 3)
                    tmp++;
                if(tmp>cnt[v][id]){
                    d[v][id]=d[u][label]+w;
                    cnt[v][id]=tmp; flag=1;
                }
            }
            node next;
            next.u=v; next.label=id;
            if(!vis[v][id] && flag){
                vis[v][id]=1; Q.push(next);
            }
        }
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    int T,kase=0;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d%d",&n,&m);
        while(m--){
            int u,v,w,id;
            scanf("%d%d%d%s",&u,&v,&w,str);
            if(str[0] == 'L') id=0;
            if(str[0] == 'O') id=1;
            if(str[0] == 'V') id=2;
            if(str[0] == 'E') id=3;
            addedge(u,v,w,id); addedge(v,u,w,id);
        }
        spfa();
        ll ans1=d[n][3],ans2=cnt[n][3];
        printf("Case %d: ",++kase);
        if(ans1 == inf || ans2 == 0)
            printf("Binbin you disappoint Sangsang again, damn it!\n");
        else
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %I64d LOVE strings at last.\n",ans1,ans2);
        
    }
    return 0;
}
/*
1
1 4
1 1 1 L
1 1 1 O
1 1 1 V
1 1 1 E
*/


hdu4360 As long as Binbin loves Sangsang spfa变形

标签:spfa

原文地址:http://blog.csdn.net/u011645923/article/details/43167303

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