以前做过这套题目

这个题又重新写了: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. git pull VS git fetch&merge(good)

    从图中可以看到,git fetch和git pull的区别, git fetch 不会自动的将结果merge到本地,只是将远程版本同步到本地版本库,而不会merge到本地副本. git pull  将 ...

  2. java自学者的福音

    谈到自学对于程序员来说并不陌生,自从我们离开校门就开始了自学之路.这一路上绝大部分都是 百步止于九十 步, 不是因为他们不够坚持,而是没有找到学习的方法和资源.当然这一路上我也走得很辛苦,刚毕业后自学 ...

  3. twrp gui/actions.cpp 中的功能实现 tw_action ,tw_action_param ,第二章

    继续分析 twrp ui.xml中的相关内容,以<page name="reboot">为讲解内容 <object type="button" ...

  4. C++ Primer 学习笔记_2_高速入口(继续)

     P15习题 //题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果 #include <iostream> int main() { std::cout <&l ...

  5. mod_python模块安装

    两.mod_python 1.性能 使用mod_python的主要优势在于比传统CGI更高的性能. 一个測试,使用在Pentium 1.2GHz的机器上执行Red Hat Linux 7.3.使用4种 ...

  6. Android网络图片显示在ImageView 上面

    在写这篇博文的时候,我參与了一个项目的开发,里面涉及了非常多网络调用相关的问题,我记得我在刚刚開始做android项目的时候,以前就遇到这个问题,当时在网上搜索了一下,发现了一篇博文,如今与大家分享一 ...

  7. Shuttle ESB 实践

    http://blog.csdn.net/liu765023051/article/category/2482069

  8. 使用GDB在远程开发机上调试

    由于一些环境限制,很多学生很可能需要在开发机器上调试.但是,由于对计算机资源的开发限制.在本地的直接机的发展GDB环境配置问题已经成为,其实,我们可以利用这段时间GDB自带gdbserver工具将能够 ...

  9. Java他们其中一个IO(一)

    1.I/O 操作的目标 其中从数据源读取数据,和写数据到的目标位置数据. 2.IO 的分类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTc ...

  10. 谷歌上不去,长期的解决方案。在稳定高速Google和Gmail

    对稳定Google神器 国内Google很不稳定,缓慢并经常上不去,由"我想去Google",安全和稳定的使用Google.Gmail.Google+所以通常需要特殊的手段岗位胜任 ...