HDU 4360 As long as Binbin loves Sangsang(最短路)

来源:互联网 发布:js 删除指定的tr 编辑:程序博客网 时间:2024/04/30 06:05

题意:问从1到n的最短路,所走的路必须是LOVELOVE,而且是完整的LOVE。就是道最短路的题。

这题比较坑,让人无语。

提供测试数据吧;

2
1 0
1 4
1 1 9 L
1 1 5 O
1 1 2 V
1 1 3 E
答案:

Binbin you disappoint Sangsang again, damn it!

Cute Sangsang, Binbin will come with a donkey after travelling 19 meters and finding 1 LOVE strings at last.

还有,这里的有一组数据超int ,要用long long。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;const int N = 1320;const int M = 13529;const int INF =0x3f3f3f3f;int n,m;long long dis[N][5];bool visit[N][5];struct LL{    int to,dis,se,nex;} L[M<<1];int F[N],cnt;void add(int f,int t,int d,int se){    L[cnt].dis = d;    L[cnt].se = se;    L[cnt].to = t;    L[cnt].nex = F[f];    F[f] = cnt++;}void init(){    scanf("%d%d",&n,&m);    int f,t,d,se;    memset(F,0,sizeof(F));    cnt= 1;    char tt[3];    for(int i=0; i<m; i++)    {        scanf("%d%d%d%s",&f,&t,&d,tt);        if(tt[0]=='L') se = 0;        if(tt[0]=='O') se = 1;        if(tt[0]=='V') se = 2;        if(tt[0]=='E') se = 3;        add(f,t,d,se);        add(t,f,d,se);    }}struct nod{    int v,se,num;long long dis;    bool operator<(const nod t) const    {        if(dis==t.dis) return num<t.num;        return dis>t.dis;    }};priority_queue<nod> que;int nums[N][5];void solve(int c){    while(!que.empty()) que.pop();    for(int i=0;i<=n;i++)    {        for(int j=0;j<5;j++) dis[i][j] = 0x3f3f3f3f3f3f3f3fll;    }    memset(visit,false,sizeof(visit));    memset(nums,0,sizeof(nums));    nod e,t;    e.v=1,e.dis=0,e.se=0,e.num=0;    que.push(e);    dis[1][0] = INF;    while(!que.empty())    {        e = que.top();        que.pop();        if(visit[e.v][e.se]) continue;        visit[e.v][e.se] = true;        if(e.dis==0)        {            visit[e.v][e.se] = false;        }        //dis[e.v][e.se] = e.dis;        if(e.v==n&&e.se ==0&&e.dis>0)        {            cout<<"Case "<<c<<": Cute Sangsang, Binbin will come with a donkey after travelling "<<e.dis<<" meters and finding "<<e.num<<" LOVE strings at last."<<endl;            //printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %d meters and finding %d LOVE strings at last.\n",c++,e.dis,e.num);            return ;        }        for(int i=F[e.v]; i; i=L[i].nex)        {            int to = L[i].to;            if(L[i].se!=e.se) continue;            int se= (e.se+1)%4;            if(se==0) t.num = e.num+1;                else t.num = e.num;            if(!visit[to][se]&&(dis[to][se]>e.dis+L[i].dis||(nums[to][se]<t.num&&dis[to][se]==e.dis+L[i].dis)))            {                dis[to][se]=e.dis+L[i].dis;                t.dis = dis[to][se];                t.se = se;                t.v = to;                nums[to][se] = t.num;                que.push(t);            }        }    }    //printf("Case %d: Binbin you disappoint Sangsang again, damn it!\n",c);    cout<<"Case "<<c<<": Binbin you disappoint Sangsang again, damn it!"<<endl;}int main(){    freopen("in.txt","r",stdin);    int cas,T=1;    scanf("%d",&cas);    while(cas--)    {        init();        solve(T++);    }    return 0;}


原创粉丝点击