题意:

给你一幅图,然后起点终点,然后有一个条件是可以使某条边的花费减半,求最短路的最小花费。

思路:

(来自大哥)

最短路的时候多一维,途中是否有花费减半的边;

然后转移,如果上一条有减半的,这一条一定只能转移到不能减半,上一条没有减半的,这一条可以减半也可以不减半。

具体处理就是一个二维的处理,网上说分层图,其实我感觉就是DP的思想,还有有一种从一张图跑到另一张图的feel。

后来wa了,就是挫在初始化,还有数据要LL,哎,艹!;

贴一发自己的挫code…

#include<cstdio>
#include<iostream>
#include<queue>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#define LL long long const int N=5e5+10; struct asd{
int to;
LL w;
int next;
};
asd q[N*4];
int head[N*4],tol;
int n,m; map<string,int>mp;
int tot; int Getpoint(string ss)
{
if(mp.find(ss)!=mp.end())
return mp[ss];
return mp[ss]=++tot;
} void init()
{
memset(head,-1,sizeof(head));
mp.clear();
tol=0;
tot=0;
} void add(int a,int b,int c)
{
q[tol].to=b;
q[tol].w=c;
q[tol].next=head[a];
head[a]=tol++;
} typedef pair<int,int> PP;
queue<PP>que; const int M=1e5+10;
const long long INF=1e15;
LL dis[N][2];
bool vis[N][2];
int num[N][2];
LL spfa(int s,int t)
{
while(!que.empty())
que.pop();
for(int i=1;i<=n;i++)
{
dis[i][0]=INF;
dis[i][1]=INF;
vis[i][0]=false;
vis[i][1]=false;
num[i][0]=0;
num[i][1]=0;
}
dis[s][0]=dis[s][1]=0;
num[s][0]=num[s][1]=1;
vis[s][0]=vis[s][1]=true;
que.push(make_pair(s,0));
que.push(make_pair(s,1));
while(!que.empty())
{
PP u=que.front();
que.pop();
vis[u.first][u.second]=0;
for(int v=head[u.first];v!=-1;v=q[v].next)
{
int i=q[v].to;
if(u.second==1)
{
if(dis[i][1]>dis[u.first][u.second]+q[v].w)
{
dis[i][1]=dis[u.first][u.second]+q[v].w;
if(!vis[i][1])
{
vis[i][1]=1;
num[i][1]++;
if(num[i][1]>=tot)
return -1;
que.push(make_pair(i,1));
}
}
}
else
{
if(dis[i][0]>dis[u.first][u.second]+q[v].w)
{
dis[i][0]=dis[u.first][u.second]+q[v].w;
if(!vis[i][0])
{
vis[i][0]=1;
num[i][0]++;
if(num[i][0]>=tot)
return -1;
que.push(make_pair(i,0));
}
}
if(dis[i][1]>dis[u.first][u.second]+q[v].w/2)
{
dis[i][1]=dis[u.first][u.second]+q[v].w/2;
if(!vis[i][1])
{
vis[i][1]=1;
num[i][1]++;
if(num[i][1]>=tot)
return -1;
que.push(make_pair(i,1));
}
}
}
}
}
if(dis[t][1]==INF)
return -1;
return dis[t][1];
} int main()
{
while(~scanf("%d%d",&n,&m))
{
string x,y;
int w;
init();
for(int i=0;i<m;i++)
{
cin>>x>>y>>w;
int xx=Getpoint(x);
int yy=Getpoint(y);
//printf("%d %d\n",xx,yy);
add(xx,yy,w);
}
cin>>x>>y;
if(mp.find(x)==mp.end()||mp.find(y)==mp.end())
{
puts("-1");
continue;
}
int s=Getpoint(x);
int t=Getpoint(y);
//printf("%d %d\n",s,t);
printf("%lld\n",spfa(s,t));
}
return 0;
}

HDU 3499【最短路】的更多相关文章

  1. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  2. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  5. Flight HDU - 3499 (分层最短路)

    Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to ...

  6. HDU2112 HDU Today 最短路+字符串哈希

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu 2544 最短路

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...

  8. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  9. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

随机推荐

  1. iOS非常全的第三方库

    iOS ● 非常全的三方库.插件.大牛博客等等   github排名:https://github.com/trending, github搜索:https://github.com/search. ...

  2. [ios]objective-c中Category类别(扩展类)专题总结

    本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/   objective-c类别的作用?通过类别的方式, ...

  3. Hibernate中的HQL语言

    一.HQL语言简介 HQL全称是Hibernate Query Language,它提供了是十分强大的功能,它是针对持久化对象,直接取得对象,而不进行update,delete和insert等操作.而 ...

  4. src/github.com/mongodb/mongo-go-driver/mongo/cursor.go 游标的简洁实用

    src/github.com/mongodb/mongo-go-driver/mongo/cursor.go // Copyright (C) MongoDB, Inc. 2017-present./ ...

  5. Netty 100万级高并发服务器配置

    前言 每一种该语言在某些极限情况下的表现一般都不太一样,那么我常用的Java语言,在达到100万个并发连接情况下,会怎么样呢,有些好奇,更有些期盼. 这次使用经常使用的顺手的netty NIO框架(n ...

  6. 怎样使用alsa API

    翻译文章的链接: http://equalarea.com/paul/alsa-audio.html 关于怎么使用ALSA API教程 这份文档帮助对ALSA API使用入门.不是一个完整的ALSA ...

  7. break和continue 都是指的最接近的内层循环

    break和continue 都是指的最接近的内层循环

  8. mongodb给我们提供了fsync+lock机制把数据暴力的刷到硬盘上

    能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求. fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实 ...

  9. python string写入二进制文件——直接wb形式open file,再write string即可

    4 down vote accepted You misunderstood what \xhh does in Python strings. Using \x notation in Python ...

  10. 登录加密 md5

    实现账户和密码登录的加密 https://github.com/AndreasPizsa/md5-jkmyers