以前做过这套题目

这个题又重新写了:http://www.cnblogs.com/jh818012/archive/2013/05/05/3182681.html

还是以前的思路

一直错在一个地方:决策的时候,如果没有走过,直接更新,如果走过,总是选最小值。如果走的是小于mid值的边,那么用tmp = max(dp[x][u] , e[i].val) 更新。

代码比以前好多了好像、

#define maxn 1005

int dp[maxn][maxn];
int n,m,k;
struct node
{
int v,next;
int val;
int f;
};
node e[ * ];
int head[maxn];
int cnt ;
void init()
{
cnt = ;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w)
{
e[cnt].v = v ;
e[cnt].val = w;
e[cnt].next = head[u] ;
head[u] = cnt ++ ; e[cnt].v = u ;
e[cnt].val = w ;
e[cnt].next = head[v];
head[v] = cnt ++ ;
return ;
}
bool vis[maxn]; void dfs(int u)
{
vis[u]= ;
for(int i = head[u]; i != - ; i = e[i].next )
if(!vis[e[i].v] )
dfs(e[i].v);
} queue< pair<int,int> > q;
#define pii pair<int,int>
bool vs[maxn][maxn]; void spfa()
{
pii cur,tt;
int u , x;
while(!q.empty()) q.pop();
dp[][] = ;
vs[][] = ;
q.push(make_pair(,));
while(!q.empty())
{
cur = q.front();
q.pop();
u = cur.first;
x = cur.second;
for(int i = head[u]; i != - ; i = e[i].next )
{
if(e[i].f == )
{
tt = make_pair(e[i].v , x + );
if(x + > k ) continue;
// printf("%d %d %d %d\n",u,x,e[i].v,x+1);
//int tmp = max(dp[e[i].v][x+1],dp[u][x]);
if((dp[e[i].v][x+] == - && dp[u][x] >= )|| ( dp[e[i].v][x+] != - && dp[u][x] >= && dp[u][x] < dp[e[i].v][x+] ) )
{
dp[e[i].v][x+] = dp[u][x] ;
if(!vs[tt.first][tt.second])
{
vs[tt.first][tt.second] = ;
q.push(tt);
}
}
}
else if(e[i].f == )
{
tt = make_pair(e[i].v,x);
if(x > k ) continue;
// printf("%d %d %d %d\n",u,x,e[i].v,x);
int tmp = max(e[i].val , dp[u][x] );
if(dp[e[i].v][x] == - || tmp < dp[e[i].v][x])
{
dp[e[i].v][x] = tmp;
if(!vs[tt.first][tt.second])
{
vs[tt.first][tt.second] = ;
q.push(tt);
}
}
}
}
vs[u][x] = ;
}
//if(dp[n][k] >= 0 ) puts("asjklsajk");
}
int check()
{
memset(dp,-,sizeof(dp));
memset(vs,,sizeof(vs));
spfa();
//printf("dp[n][k] = %d\n",dp[n][k]);
return dp[n][k];
}
void build(int x)
{
for(int i = ; i <= n ; i ++ )
{
for(int j = head[i] ; j != - ; j = e[j].next)
if(e[j].val > x )
e[j].f = ;
else e[j].f = ;
}
}
int _max;
int main()
{
int u,v,w;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
init();
_max = ;
for(int i = ; i <= m ; i ++ )
{
scanf("%d%d%d",&u,&v,&w);
_max = max(_max , w);
add(u,v,w);
}
dfs();
if(!vis[n])
{
printf("-1\n");
continue;
}
int left , right ,mid ;
left = ;
right = _max ;
int ans ;
int res;
ans = 0x3f3f3f3f;
while(left < right)
{
mid = (left + right ) / ;
//printf("mid = %d\n",mid);
build(mid);
res = check();
//printf("res = %d\n",res);
if(res == - )
{
// printf("%d\n",left);
left = mid + ;
}
else
{
ans = min(ans , res);
right = mid ;
}
}
printf("%d\n",ans);
}
return ;
} /*
8 10 2
1 2 1
1 3 10
2 3 2
3 4 11
3 5 3
4 5 10
4 8 12
4 6 9
5 7 4
6 7 5
*/

Telephone Lines USACO 月赛的更多相关文章

  1. USACO Telephone Lines

    洛谷 P1948 [USACO08JAN]电话线Telephone Lines https://www.luogu.org/problem/P1948 JDOJ 2556: USACO 2008 Ja ...

  2. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  3. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  4. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  5. poj3662 Telephone Lines【最短路】【二分】

    http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. [POJ] 3362 Telephone Lines

    Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7978 Accepted: 2885 Descr ...

  7. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  8. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  9. BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 892  Solved: ...

随机推荐

  1. 《实验数据的结构化程序设计》 2.4.4Calendar个人意见,寻求指引

    题目大意: 制作一个日历系统,输入年份.一些周年纪念日,及服务要求日期,依据要求日期输出,输出重要程度小于发生日期的周年纪念日. 题目地址: UVA  145 个人见解: 纯模拟,在闰年,输出顺序及输 ...

  2. BootStrap -- Grid System

    <script src="jquery.1.9.js"></script> <script src="js/bootstrap.min.js ...

  3. Spring、Spring MVC、MyBatis

    Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Sp ...

  4. linux 下上传 datapoint数据到yeelink 修改版本

      /*client.c*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  5. [LeetCode228]Summary Ranges

    题目: Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...

  6. nyoj 题号12 水厂(两)——南阳oj

    标题信息: 喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有一块草坪.横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=1000 ...

  7. Oracle 六闪回技术,flashback

    Flashback 技术基于Undo segment基于内容的, 因此,限制UNDO_RETENTON参数. 要使用flashback 特征,您必须启用自己主动撤销管理表空间. 在Oracle 11g ...

  8. poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)

    意甲冠军:给定一个有向图有m单向边缘.免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求 弱联通重量. 算法: 缩点求强连通分量.然后又一次建图.推断新图是否是一条单链,即不能分叉,假设分 ...

  9. [生产环境数据恢复]innobackupex: fatal error: OR no &#39;datadir&#39; option in group &#39;mysqld&#39; in MySQL options

    1 运行恢复命令  [xxx@xxx-c001db1 tmp]$ time /usr/bin/innobackupex --rsync --user="user" --passwo ...

  10. CSS 之 光进入光

    一个.概念 css,层叠样式表(英语:Cascading Style Sheets.简写CSS).又称串样式列表.层次结构式样式表文件,一 种用来为结构化文档(如HTML文档或XML应用)加入样式(字 ...