算出从点1到点n的最短路径。

/* ***********************************************
Author :guanjun
Created Time :2016/6/23 22:58:19
File Name :1019.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dis[][];
int n,m;
int lowcost[];
int vis[];
int dij(){
cle(vis);
vis[]=;
memset(lowcost,INF,sizeof lowcost);
for(int i=;i<=n;i++)lowcost[i]=dis[][i];
lowcost[]=;
int k,Min;
for(int i=;i<=n;i++){
Min=INF;
for(int j=;j<=n;j++){
if(lowcost[j]<Min&&!vis[j])Min=lowcost[j],k=j;
}
if(Min==INF)break;
vis[k]=;
for(int j=;j<=n;j++){
if(lowcost[j]>lowcost[k]+dis[k][j]&&!vis[j])
lowcost[j]=lowcost[k]+dis[k][j];
}
}
return lowcost[n];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,x,y;
ll z;
cin>>T;
for(int t=;t<=T;t++){
cin>>n>>m;
memset(dis,INF,sizeof dis);
for(int i=;i<=m;i++){
cin>>x>>y>>z;
if(dis[x][y]>z)dis[y][x]=dis[x][y]=z;
}
int ans=dij();
if(ans==INF){
printf("Case %d: Impossible\n",t);
}
else printf("Case %d: %d\n",t,ans); }
return ;
}

Lightoj 1019 - Brush (V)的更多相关文章

  1. Light OJ 1019 - Brush (V)(图论-dijkstra)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...

  2. Lightoj 1016 - Brush (II)

    After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to ...

  3. lightoj 1019

    裸的最短路 dijkstra #include<cstdio> #include<string> #include<cstring> #include<ios ...

  4. lightOJ 1017 Brush (III) DP

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1017 搞了一个下午才弄出来,,,,, 还是线性DP做的不够啊 看过数据量就知道 ...

  5. LightOJ 1017 - Brush (III) 记忆化搜索+细节

    http://www.lightoj.com/volume_showproblem.php?problem=1017 题意:给出刷子的宽和最多横扫次数,问被扫除最多的点是多少个. 思路:状态设计DP[ ...

  6. [LightOJ 1018]Brush (IV)[状压DP]

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1018 题意分析:平面上有不超过N个点,如今能够随意方向划直线将它们划去,问:最少要划 ...

  7. Lightoj 1018 - Brush (IV)

    1018 - Brush (IV)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Muba ...

  8. Lightoj 1017 - Brush (III)

    1017 - Brush (III)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sam ...

  9. light oj 1019【最短路模板】

    1019 - Brush (V) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Tanvir r ...

随机推荐

  1. luoguT21777

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  2. Android记录2013年10月20日

    1. ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connect ...

  3. 在java中获取Map集合中的key和value值

  4. Python 双向队列Deque、单向队列Queue 模块使用详解

    Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...

  5. HDU 4821 字符串hash

    题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...

  6. codeforces 303 div2 E

    赤裸裸的最短路,需要注意下枚举过程就好了.直接贴上别人的代码,发现他的代码挺符合我的风格,以后就这样写了. #include <bits/stdc++.h> ]; ]; ]; ]; vec ...

  7. Linux(4):文件属性

    文件属性: # 重点: 磁盘空间不足 和 软链接与硬链接的区别 查看文件的属性: # ls lhi 文件 [root@NEO ~]# ls -lhi /etc/hosts 130078 -rw-r-- ...

  8. VS Code 列编辑功能说明

    新版本v1.13.1或者附近的版本中的列编辑功能已经调整. 一.多光标插入功能 Alt+鼠标左键,添加多光标输入 二.自由多行选择 Alt键+鼠标左键拖动选择各行的部分内容 三.列选择 Shift+A ...

  9. Borg Maze-POJ3026(bfs+最小生成树)

    http://poj.org/problem?id=3026 如果一个一个普通搜处理不好的话会超时  可以连到一块搜 我觉得这个方法特别好 #include<stdio.h> #inclu ...

  10. Java时间戳转化为今天、昨天、明天(字符串格式)

    原文:http://www.open-open.com/code/view/1435301895825 时间戳,相信大家一定都不陌生,服务器经常会传回来时间戳,需要我们对时间戳进行处理.各种麻烦不断, ...