PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;
int n,m,k,ds;
struct Edge
{
int u,v;
int dis;
}e[*maxn];
vector<int>g[maxn];
int tot;
int flag[maxn],dis[maxn];
double ans1,ans2;
int ans;
int MIN; int get(char *s)
{
int num=;
if(s[]!='G')
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
}
else
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
num=num+n;
}
return num;
} void SPFA(int s)
{
queue<int>Q;
memset(flag,,sizeof flag);
for(int i=;i<=n+m;i++) dis[i]=INF;
Q.push(s); flag[s]=; dis[s]=;
while(!Q.empty())
{
int head=Q.front(); Q.pop(); flag[head]=;
for(int i=;i<g[head].size();i++)
{
int id=g[head][i];
if(dis[head]+e[id].dis<dis[e[id].v])
{
dis[e[id].v]=dis[head]+e[id].dis;
if(flag[e[id].v]==)
{
flag[e[id].v]=;
Q.push(e[id].v);
}
}
}
}
} int main()
{
scanf("%d%d%d%d",&n,&m,&k,&ds); tot=;
for(int i=;i<=k;i++)
{
char u[],v[]; int dis;
scanf("%s%s%d",u,v,&dis);
e[tot].u=get(u),e[tot].v=get(v);
e[tot].dis=dis;
g[get(u)].push_back(tot),tot++; e[tot].u=get(v),e[tot].v=get(u);
e[tot].dis=dis;
g[get(v)].push_back(tot),tot++;
} ans1=1.0*INF;
ans2=1.0*INF;
MIN=; for(int i=n+;i<=n+m;i++)
{
SPFA(i);
int sum=,MIN_NOW=INF;
for(int j=;j<=n;j++)
{
if(dis[j]>ds) {sum=-;break;}
else
{
sum=sum+dis[j];
MIN_NOW=min(MIN_NOW,dis[j]);
}
}
if(sum==-) continue; if(MIN_NOW>MIN)
{
MIN=MIN_NOW;
ans2=1.0*sum/n;
ans1=1.0*MIN;
ans=i;
} else if(MIN_NOW==MIN&&1.0*sum/n<ans2)
{
ans2=1.0*sum/n;
ans=i;
}
}
if(MIN==) printf("No Solution\n");
else
{
printf("G%d\n",ans-n);
printf("%.1lf %.1lf\n",ans1,ans2);
}
return ;
}
PAT (Advanced Level) 1072. Gas Station (30)的更多相关文章
- 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)
题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072. Gas Station (30)
先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 1072 Gas Station (30)(30 分)
A gas station has to be built at such a location that the minimum distance between the station and a ...
随机推荐
- 图片(img标签)大小自适应
$(function(){ var myimg,oldwidth,oldheight; var maxwidth=249; var maxheight=187; var imgs = document ...
- hdu_4714_Tree2cycle(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...
- 指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效
以下内容来自互联网 (1)web: 需要在客户端配置文件的中增加connectionString节点,此节点描述了EntityClient的连接信息. 例如: 在web.config的中增加conne ...
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
- Android BuildConfig.DEBUG的妙用
在Android开发中,我们使用android.util.Log来打印日志,方便我们的开发调试.但是这些代码不想在发布后执行,我们并不想在软件发布后调试日志被其他开发者看到,现在我的方法是设置一个全局 ...
- 全文检索工具推荐FileLocator
全文检索工具推荐FileLocator https://www.baidu.com/link?url=_vaDZaJ_OePrAX-BTUD5hjTymnvN7_1oIAnWyS25hqxAg0nUH ...
- hdu5269 ZYB loves Xor I
分治法和字典树都可以,都是递归,但字典树耗内存 从第一bit开始,若相同则xor为0,分到同一部分,不相同则统计,且此时lowbit为这一bit,最后结果要乘以2 /*分治法*/ #include&l ...
- List、Set、Map集合存放null解析及HashMap、Hashtable异同点解析
1.List.Set.Map集合存放null解析: @Test public void CollectionTest() { // 测试List List<Object> list = n ...
- NSUserDefault -- synchronize 浅析
NSUserDefault的使用比较简单:NSUserDefaults *mySettingData = [NSUserDefaults standardUserDefaults]; 创建NSUse ...
- linux和windows之间上传 下载文件 非ftp方式
用 命令 rz 上传 sz 下载 文件夹加上 -r rz上传替换时用 -y 谁用谁知道 两台linux传 : scp -r 文件夹 username@ip:路径 (如果传输文件就 ...