HDU 3499【最短路】
题意:
给你一幅图,然后起点终点,然后有一个条件是可以使某条边的花费减半,求最短路的最小花费。
思路:
(来自大哥)
最短路的时候多一维,途中是否有花费减半的边;
然后转移,如果上一条有减半的,这一条一定只能转移到不能减半,上一条没有减半的,这一条可以减半也可以不减半。
具体处理就是一个二维的处理,网上说分层图,其实我感觉就是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【最短路】的更多相关文章
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- 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 ...
- HDU2112 HDU Today 最短路+字符串哈希
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
随机推荐
- MySQL 存储过程 (3)
以下介绍下像数据库循环插入数据操作 第一步:建立存储过程用到的信息表
- Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移
B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to t ...
- WEBRTC开发入门
WEBRTC "WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌2010年以6 ...
- [Phoenix] 一、快速入门
Phoenix是一个开源的HBASE SQL层.Phoeinx可以用标准的JDBC API替代HBASE client API来创建表,插入和查询HBASE中的数据. Phoenix作为应用层和HBA ...
- Java版TicTacToe
MainFrame.java package com.bu_ish; import java.awt.BorderLayout; import java.awt.Color; import java. ...
- Could not find com.android.tools.lint:lint-gradle:26.1.2.
allprojects { repositories { flatDir { dirs 'libs' } jcenter() google() }}
- xml 基础属性
xml属性 对应的方法 说明 android:alpha setAlpha(float) 设置组件的透明度(0——1) android:background setBackgroundResource ...
- leetcode 747. Largest Number At Least Twice of Others
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- dij+堆优化
写这个dij+堆优化的原因是有些地方卡SPFA,只能搞这个: 香甜的奶油: #include<iostream> #include<cstdio> #include<cs ...
- Retina屏幕下image-set
实现Retina屏幕下图像的显示方法,还特别给我截取了一段代码: .soso .logo .qqlogo { display: block; width: 134px; height: 44px; b ...